【问题标题】:Cannot read property 'moduleType' of undefined while npm startnpm start 时无法读取未定义的属性“moduleType”
【发布时间】:2018-04-13 15:32:42
【问题描述】:

我正在开发支持使用 angular cli (1.7.3) 进行服务器端渲染的应用程序。

在运行命令npm start build 时会成功创建,但在服务器上运行应用程序时会抛出错误显示

无法读取未定义的属性“moduleType”

我假设错误来自server.ts 文件,我在该文件中导入AppServerModuleNgFactory,如下所示:

const {  AppServerModuleNgFactory } = (module as any).exports;
// import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'

PS:这适用于 Angular 4.4.0,但不适用于 5.2.0

如果需要更多信息,请告诉我。

【问题讨论】:

    标签: angular


    【解决方案1】:

    在谷歌搜索数小时后解决了这个问题。

    实际上问题出在 ng5 中,他们在构建时更改了一些配置,例如删除 ngfactory 等。 所以server.ts 在服务器端渲染时无法找到AppServerModuleNgFactory 模块。

    所以如果找不到这样的路径,我必须从新路径更改位置

    const {  AppServerModuleNgFactory } = (module as any).exports;
    ....
    some code 
    ....
    
    if ( typeof(AppServerModuleNgFactory) == 'undefined') {
      const { AppServerModuleNgFactory } = require('../dist/out-tsc/src/app/app.server.module.ngfactory');
      renderModuleFactory(AppServerModuleNgFactory, opts)
      .then(html => callback(null, html));
    } else {
      renderModuleFactory(AppServerModuleNgFactory, opts)
      .then(html => callback(null, html));
    }
    

    【讨论】:

    • @Pradeep Jain 你能描述一下吗,因为我没有这个文件'../dist/out-tsc/src/app/app.server.module.ngfactory',我有文件'./dist/server/main.bundle'
    • 如果你没有这个,你可以尝试在你的配置中使用'./dist/server/main.bundle'这个路径来获取模块。
    • @PardeepJain 我已经尝试过使用 '../dist/out-tsc/src/app/app.server.module.ngfactory' 和 './dist/server/main' 作为我尚未生成捆绑文件,但它仍然无法正常工作我正在使用 angular 6
    • 对于 Angular6,我猜他们已经改变了 dist 文件夹结构。
    猜你喜欢
    • 2019-11-06
    • 2016-09-12
    • 1970-01-01
    • 2018-07-17
    • 2022-07-03
    • 1970-01-01
    • 2017-12-30
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多