我真的很喜欢这个article。它回顾了实现SSR的不同方法,并为您提供了足够的解释。
我奋斗了8个小时后解决了问题。
您不能在服务器上使用react的tsconfig文件。
我必须制作一个新的配置文件。 (server.tsconfig.json)
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"*": ["*", "./src/@types/*"]
},
"outDir": "./dist",
"target": "es5",
"typeRoots": ["./src/@types", "./node_modules/@types"],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noImplicitAny": false,
"moduleResolution": "node",
"strictNullChecks": false,
"jsx": "react",
"noEmit": true
},
"exclude": ["node_modules"]
}
并在运行ts节点时指示此配置
ts-node --project server.tsconfig.json --require tsconfig-paths/register server/index.js
如果使用自定义类型,则必须在服务器文件上添加类型的引用
/// <reference path="../src/@types/global.d.ts" />
我希望这可以节省您的时间