2017-09-05 47 views
1

我是Angular2的新手,对于将它合并到ASP.NET Web应用程序中是一个新手。 当我尝试在我的Angular2 ASP.NET Web应用程序中以调试模式构建时,出现此错误。在发布模式下构建时,我遇到了同样的错误。Angular2在ASP.NET中 - 窗口没有定义

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0] An unhandled exception has occurred while executing the request Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Prerendering failed because of error: ReferenceError: window is not defined at D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:89981:21 at Object.<anonymous> (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:90732:3) at Object.<anonymous> (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:90734:30) at __webpack_require__ (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:21:30) at Object.<anonymous> (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:85385:1) at Object.<anonymous> (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:85386:30) at __webpack_require__ (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\vendor.js:21:30) at Object.<anonymous> (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\main-server.js:20466:42) at __webpack_require__ (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\main-server.js:20:30) at Object.exports.REPLACEMENT_CHARACTER (D:\Projects\BryterOpsAdmin\BryterOpsAdmin\ClientApp\dist\main-server.js:398:79) Current directory is: D:\Projects\BryterOpsAdmin\BryterOpsAdmin

我刚刚做了一些微不足道的变化对模型类的MVC的一面,所以,当我得到这个错误我恢复了我的变化,并再次尝试。 我仍然收到此错误。根据我的源代码控制,自上次正常工作以来绝对没有发生任何变化。 什么可能导致此错误?我完全被难住甚至试图寻找什么。

我搜索了StackOverflow的任何相关的东西,但是我没有发现任何适用于asp.net的东西,除了这个问题:Prerendering failed because of error: ReferenceError: window is not defined 这是从未回答。

我试图删除隐藏的obj和bin文件夹无济于事。

预先感谢您。

更新: 我试着直接导航到我网站上的一个页面,它成功地完成了。看来Angular2不再自动路由。我仍然通过MVC('Home/Index')触及Home Controller和Home Index.cshtml视图,所以这看起来像是一个Angular2问题。

这是我的angular2路由看起来像。

`

RouterModule.forRoot([ 
      { path: '', redirectTo: 'home', pathMatch: 'full' }, 
      { path: 'home', component: HomeComponent }, 
      { path: 'counter', component: CounterComponent }, 
      { path: 'fetch-data', component: FetchDataComponent }, 
      { path: 'bryterUsers', component: BryterUsersComponent }, 
      { path: 'adminUsers', component: AdminUsersComponent }, 
      { path: 'retailers', component: RetailersComponent }, 
      { path: 'providers', component: ProvidersComponent }, 
      { path: '**', redirectTo: 'home' } 
     ])` 

UPDATE2:我现在不能再导航到任何其他页面。无论我走到哪里,我都会得到'窗口未定义'的错误。我的代码中没有改变任何内容。我完全被难住了。任何帮助将不胜感激。

回答

1

我找到了答案在这里,虽然我不知道这是否是正确的答案:

Prerendering failed because of error: ReferenceError: Event is not defined with PrimeNG AutoComplete

这里是他们做了什么:

“我能够解决这个改变ASP.Net核心模板生成的Index.cshtml页面中的asp-prerender标签。我错过了关于ASP.Net核心模板从我的问题。

我改变了标签从

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>

<app asp-ng2-prerender-module="ClientApp/dist/main-server">Loading...</app>

这个固定我的问题,但我不知道这是不是最好的做法。

+0

没有真正使用的解决方案,但黑客攻击。这会禁用服务器端呈现。 – Ahmad

1

当您向app.module.shared.ts添加某些内容时,会导致此错误,该内容取决于window变量,该变量仅在不在服务器上的浏览器中可用。它很难找出Hit n Trial是什么导致这个问题,但现在唯一的解决方案。

找到那个东西,并将它移动到app.module.browser.ts

@techgeekster的解决方案将工作,但我会禁用服务器端渲染。

参考:GitHub Issue

0

在我来说,它是使用window.location.origin而在服务器端预渲染。

查找的window.location.origin任何使用,并检查它仅在客户端