2017-09-05 48 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任何使用,並檢查它僅在客戶端