gpt4 book ai didi

node.js - VueJs : Error with server side rendering and Typescript

转载 作者:IT老高 更新时间:2023-10-28 23:17:25 26 4
gpt4 key购买 nike

我正在尝试使用服务器端渲染 (SSR) 和 Typescript 构建堆栈。一切似乎都很好,但我得到了错误:TypeError: Cannot read property 'render' of undefined。这是完整的堆栈跟踪:

TypeError: Cannot read property 'render' of undefined
at normalizeRender (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6621:19)
at render (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6840:5)
at Object.renderToString (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6871:9)
at /Users/shoun/Documents/repositories/vuejs-ssr-typescript/dist/server.js:16:14
at Layer.handle [as handle_request] (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/layer.js:95:5)
at /Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/index.js:281:22
at param (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/index.js:354:14)
TypeError: Cannot read property 'render' of undefined
at normalizeRender (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6621:19)
at render (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6840:5)
at Object.renderToString (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/vue-server-renderer/build.js:6871:9)
at /Users/shoun/Documents/repositories/vuejs-ssr-typescript/dist/server.js:16:14
at Layer.handle [as handle_request] (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/layer.js:95:5)
at /Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/index.js:281:22
at param (/Users/shoun/Documents/repositories/vuejs-ssr-typescript/node_modules/express/lib/router/index.js:354:14)

这是我的服务器配置:

import * as express from 'express';
import * as path from 'path';
import * as VueRender from 'vue-server-renderer';
import * as fs from 'fs-extra';
import app from './assets/app';
declare var __dirname;

// Get the HTML layout
const layout = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8');

// Create a renderer
const renderer = VueRender.createRenderer();

let server = express();

server.get('/hello', function (req, res) {
res.send('Hello World!');
});

server.use('/assets', express.static(path.join(__dirname, 'assets')));

// Handle all GET requests
server.get('*', function (request, response) {
// Render our Vue app to a string
renderer.renderToString(
// Create an app instance
app(),
// Handle the rendered result
function (error, html) {
// If an error occurred while rendering...
if (error) {
// Log the error in the console
console.error(error);
// Tell the client something went wrong
return response
.status(500)
.send('Server Error')
}
// Send the layout with the rendered app's HTML
response.send(layout.replace('<div id="app"></div>', html))
}
)
});

let port = 4500;
server.listen(port, () => {
console.log(`App listening on ${port}`);
});

您可以在我的 github 存储库中找到源代码:https://github.com/sichida/vuejs-ssr-typescript .我真的需要一些帮助,因为我被困住了......

非常感谢!

最佳答案

我检查了您的存储库,问题实际上出在文件 src/assets/app.ts 中,在 createApp 函数中,您返回的对象类型为 ComponentOptionsrenderToString 采用 Vue 类型的对象。

它实际上比你现在拥有的要简单得多:

import * as Vue from 'vue';

let createApp = function () {
return new Vue({
props: ['message'],
template: '<span>{{ message }}</span>',
});
};
export default createApp;

就是这样,你只需要返回一个新的 Vue 实例。

关于node.js - VueJs : Error with server side rendering and Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com