【问题标题】:How to use an angular module that does not support Universal with an angular/cli app using SSR如何将不支持 Universal 的 Angular 模块与使用 SSR 的 angular/cli 应用程序一起使用
【发布时间】:2018-09-14 04:02:43
【问题描述】:

大家好,我一直在构建一个使用 Universal 和 SSR 的 Angular 应用程序有一段时间了,我经常会包含一个模块,该模块会导致服务器静默失败并且不知道为什么,昨晚我想通了是因为我尝试包含的模块(ngx-editor)不支持通用。

有没有办法让我在我的应用程序中包含一个不支持 Universal 的模块,例如 ngx-editor?还是我必须找到一个支持通用的?

非常感谢。

【问题讨论】:

    标签: node.js angular angular-universal angular2-aot


    【解决方案1】:

    您可以尝试不调用此模块的组件,具体取决于平台,例如通过在代码中动态检查平台 (https://angular.io/api/common/isPlatformBrowser)

    import {isPlatformBrowser} from "@angular/common";
    //...
    constructor(@Inject(PLATFORM_ID) private platformId: Object)
    {
     if(isPlatformBrowser(this.platformId))
     {
       //call module's methods/components...
     }
     else { /*server side*/ }
    }
    

    您可能还需要修改服务器模块(app.server.module.ts) 以不包含不支持 angular-universal 的模块

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 2017-05-03
      • 2020-01-13
      • 2017-05-11
      • 2022-10-07
      • 2018-12-03
      • 2016-11-10
      • 1970-01-01
      相关资源
      最近更新 更多