Home > Blockchain >  Routing error finding routing module in test angular project
Routing error finding routing module in test angular project

Time:01-06

I was running a test project on lazy loading and followed the pluralsite article exactly (as far as I can tell) but I get this error when trying to follow the link to my component. The error says it can't find the first.module.

It seems pretty simple. The article is: enter image description here

The files look like:

enter image description here

The code looks like:

app.module.ts:

enter image description here

The app-routing.module.ts:

enter image description here

first-routing.module.ts:

enter image description here

first.module.ts:

enter image description here

app.component.html:

enter image description here

I also tried changing the route in app-routing.module.ts to:

enter image description here

That just got me the same error with this new routing:

enter image description here

I looked at Sakthi's suggestion, which does work but not sure why the other format doesn't work. I also noticed that when looking at the DevTools the first-first-module.js doesn't show up as the author mentioned would appear when I hit the "First" link. It followed the path and displayed the component, but didn't show up in the network tab of DevTools. Is that because I didn't use the other format?

enter image description here

Thanks,

Tom

CodePudding user response:

You don't need to include the first.module file path in app-routing.module.ts .You have to import the first.module in app-routing.module.ts and use it like this.

import {FirstModule} from './first/first.module';
const routes: Routes = [
    {
        path: 'First',
        loadChildren: () => FirstModule
    },
@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }


CodePudding user response:

In Angular version 8, loadChildren string syntax was deprecated you can use new import() syntax

in your app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FirstModule } from './first/first.module';

const routes: Routes = [
    {
        path: 'First',
        loadChildren: () => import('./first/first.module').then(m => m.FirstModule)
    },
  ]

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }
  •  Tags:  
  • Related