2

I wanted to add lazy loaded route for admin in my project. I'm using ASP Net Core backend and Angular 6 frontend, so my output dir for compile code is: wwwRoot/Angular/dist. When I compiled project, I see that file "admin-admin-module-ngfactory.js exists there, but when I try to enter page '/admin/dash', it throw error:

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)

Here is my routing modules:

admin-routing.module.ts:

const routes: Routes = [ 
  { path: 'dash', component: AdminDashboardComponent, pathMatch: 'full'}
];


@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ],
  exports: [RouterModule],
  declarations: []
})
export class AdminRoutingModule { }

and 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 { }

UPDATE:

If I run project with dotnet run (with ng build --aot) it doesn't work, but if I launch only Angular app with ng serve --aot it works.

2
  • double check the path Commented Jul 3, 2018 at 10:59
  • I've checked path, I'm sure that is correct. Commented Jul 3, 2018 at 11:04

2 Answers 2

3

Add below line in .angular-cli.json file.

"apps": [
{
  "deployUrl": "Angular-Build-Path/",

Thanks.

Sign up to request clarification or add additional context in comments.

2 Comments

I added: "deployUrl": "/Angular/dist/", and it works! Thank you very much.
can you provide here the changes which you have made in your code, It will be easier for others to relate.
0

According deploy documentation of Angular:

If you copy the files into a server sub-folder, append the build flag, --base-href and set the appropriately.

So, you should use the --base-href parameter in .angular-cli.json file.

Optionally, you can work in development mode without -base-href configuration(in the angular-cli- file) and only set it at aplication production build.

ng build --base-href=/Angular/dist/

References:

https://angular.io/guide/deployment#simplest-deployment-possible

https://angular.io/guide/deployment#the-base-tag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.