2

I have been trying to route from my homepage to load other components HTML, it doesn't work. The homepage keeps showing up while the address changes to the one I set -

app.module.ts is the following:

@NgModule({
  declarations: [
    AppComponent,
    RegisterComponent,
    TabsComponent,
    AboutComponent,
    LessonsComponent
  ],
  imports: [
    RouterModule.forRoot([

      { path: 'tabs', component: TabsComponent},
      { path: 'register', component: RegisterComponent},
      { path: 'lessons',component: LessonsComponent},
      { path: 'about', component: AboutComponent},
      { path: 'app',component: AppComponent},
    ]),

these are my links in my app.component.html:

<div>
    <ul class="navbar">
          <li><a routerLink="/register" routerLinkActive="active">Register</a></li>
          <li><a routerLink="/tabs" routerLinkActive="active">Tabs</a></li>
          <li><a routerLink="/app" routerLinkActive="active">Home</a></li>
          <li><a routerLink="/lessons" routerLinkActive="active">Lessons</a></li>
          <li style="float:right"><a routerLink="/about" routerLinkActive="active">About</a></li>
      </ul>
</div>

For example, if I press the register link I will get to "http://localhost:4200/register", but It stays on the app.component.html

5
  • can you check if there are any errors in the browser console? If there are, navigation to new component will not run. Commented Oct 7, 2019 at 10:15
  • 1
    meaning I must fix these errors before it'll load? there are four errors. and those are concerning youtube embed videos Commented Oct 7, 2019 at 10:20
  • Can you fix them or still need help? If you use chrome, angular will provide more user friendly error messages. Probably there is a root cause which creates all those errors Commented Oct 7, 2019 at 10:22
  • I do need help, I have tried to place the <router-outlet></router-outlet> in my app.component.html but it makes my homepage show twice instead of once and the links aren't working. Commented Oct 7, 2019 at 10:24
  • Can you create a sample repo? Commented Oct 7, 2019 at 10:26

1 Answer 1

1

Angular Routing

Angular routing is not working in app.module.ts now.in this case you need to add that path and component in App.routingmodule.ts. like this....

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  { path: 'hero/:id',      component: HeroDetailComponent },
  {
    path: 'heroes',
    component: HeroListComponent,
    data: { title: 'Heroes List' }
  },
  { path: '',
    redirectTo: '/heroes',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  ...
})
export class AppModule { }
Sign up to request clarification or add additional context in comments.

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.