[英]Angular 6 Lazy loading route
我想在項目中為管理員添加延遲加載的路由。 我正在使用ASP Net Core后端和Angular 6前端,因此我的編譯代碼輸出目錄是:wwwRoot / Angular / dist。 當我編譯項目時,我看到文件“ admin-admin-module-ngfactory.js存在於其中,但是當我嘗試輸入頁面'/ admin / dash'時,它拋出錯誤:
Failed to load resource: the server responded with a status of 404 (Not Found)
Error: Uncaught (in promise): Error: Loading chunk admin-admin-module-ngfactory failed.
(error: http://localhost:5000/admin-admin-module-ngfactory.js)
Error: Loading chunk admin-admin-module-ngfactory failed.
(error: http://localhost:5000/admin-admin-module-ngfactory.js)
這是我的路由模塊:
管理員-routing.module.ts:
const routes: Routes = [
{ path: 'dash', component: AdminDashboardComponent, pathMatch: 'full'}
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [RouterModule],
declarations: []
})
export class AdminRoutingModule { }
和app-routing.module.ts:
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'error', component: ErrorComponent },
{ path: 'admin', loadChildren: './admin/admin.module#AdminModule' },
{ path: '', pathMatch: 'full', redirectTo: 'dashboard' },
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [RouterModule]
})
export class AppRoutingModule { }
更新:
如果我使用dotnet run(使用ng build --aot)運行項目,則無法使用,但是如果我僅使用ng serve --aot啟動Angular應用程序,則可以使用。
在.angular-cli.json文件中添加以下行。
"apps": [
{
"deployUrl": "Angular-Build-Path/",
謝謝。
根據Angular的部署文檔:
如果將文件復制到服務器子文件夾,請附加構建標記--base-href並進行適當設置。
因此,您應該在.angular-cli.json文件中使用--base-href參數。
(可選)您可以在沒有-base-href配置的情況下(位於angular-cli-file中)在開發模式下工作,而僅在復制生產版本中進行設置。
ng build --base-href = /角度/距離/
參考文獻:
https://angular.io/guide/deployment#simplest-deployment-possible
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.