【问题标题】:Angular 6 SSR with ASP .Net Core 2.1: window not defined带有 ASP .Net Core 2.1 的 Angular 6 SSR:未定义窗口
【发布时间】:2019-01-26 08:36:37
【问题描述】:

我正在使用带有 Angular 6.0.2 服务器端渲染的 Asp .net core 2.1。但我的问题是,当我将路由添加到我的角度应用程序时,它会在服务器端呈现页面时给我错误“未定义窗口”。 我尝试使用路由添加一个空组件并给我同样的错误。 而在没有路由的情况下使用该应用程序与 SSR 完美配合。 我无法弄清楚为什么会这样? 错误堆栈是:

    Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: 'window is not defined
ReferenceError: window is not defined
    at factory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\common\bundles\common.umd.js:5610:169)
    at _callFactory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8457:24)
    at _createProviderInstance (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8415:30)
    at resolveNgModuleDep (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8390:21)
    at _callFactory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8463:71)
    at _createProviderInstance (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8415:30)
    at resolveNgModuleDep (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8375:25)
    at NgModuleRef_.get (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:9083:20)
    at RouterInitializer.bootstrapListener (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\router\bundles\router.umd.js:5354:48)
    at C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:4677:60'

【问题讨论】:

  • https://github.com/angular/universal/issues/830 试试这个解决方案。

标签: angular asp.net-core-2.1


【解决方案1】:
if (typeof window !== 'undefined') {
  // put your browser code here
}   

因为 Window 对象没有在服务器上定义。它是浏览器对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-13
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2020-11-20
    • 2018-11-18
    相关资源
    最近更新 更多