带有ASP .Net Core 2.1的Angular 6 SSR:未定义窗口

时间:2018-08-20 08:37:19

标签: angular asp.net-core-2.1

我正在使用带有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'

1 个答案:

答案 0 :(得分:0)

if (typeof window !== 'undefined') {
  // put your browser code here
}   

使用nodejs Express Server代替Aspnet核心JavascriptServices,因为您必须为Angular Universal配置很多。