角度 SSR 类型运行时错误:无法读取未定义的属性'extend'



由于以下错误,我无法运行SSR服务器,我已经附加了main.js文件和错误日志

我遵循的步骤来生成主构建

npm run build:ssr

运行 SSR 服务器时出错

/home/suhayb/repos/hisas/web-app/dist/web-app-server/main.js:4680
dayjs_1.default.extend(relativeTime_1.default);
^
TypeError: Cannot read property 'extend' of undefined

所以似乎 dayjs 不支持 SSR.In 服务器端无法访问 DOM、窗口、本地存储... 解决方案:如果只是浏览器,请使用带有条件的dayjs特定方法。 例:

import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser} from '@angular/common';
...
export class MyComponent {
...
testBrowser: boolean;
constructor(
@Inject(PLATFORM_ID) platformId: string) {
this.testBrowser = isPlatformBrowser(platformId);
if (this.testBrowser) {
//this is only executed on the browser
}
}
...

最新更新