##Error I have implemented nested routing in my app. when application loads its shows login screen after login its redirects to admin page where further child routes exist like user, product, api etc. now when I navigate to admin it by default loads user screen but further <routeLinks>
not working and its shows this error. Error: Uncaught (in promise): Error: Cannot match any routes: 'product'
After Click Product it shows this
##Code main.ts
import { bootstrap } from '@angular/platform-browser-dynamic'; import { APP_ROUTER_PROVIDERS } from '../app/app.routes'; import { AppComponent } from '../app/app.component'; bootstrap(AppComponent, [APP_ROUTER_PROVIDERS]);
##app.component
import { Component } from '@angular/core'; import { ROUTER_DIRECTIVES } from '@angular/router'; @Component({ selector: 'demo-app', template: ` <div class="outer-outlet"> <router-outlet></router-outlet> </div> `, directives: [ROUTER_DIRECTIVES] }) export class AppComponent { }
##app.routes
import { provideRouter, RouterConfig } from '@angular/router'; import { AboutComponent, AboutHomeComponent, AboutItemComponent } from '../app/about.component'; import { HomeComponent } from '../app/home.component'; export const routes: RouterConfig = [ { path: '', component: HomeComponent }, { path: 'admin', component: AboutComponent, children: [ { path: '', component: AboutHomeComponent }, { path: '/product', component: AboutItemComponent } ] } ]; export const APP_ROUTER_PROVIDERS = [ provideRouter(routes) ];
##home.component
import { Component } from '@angular/core'; @Component({ selector: 'app-home', templateUrl:'../app/layouts/login.html' }) export class HomeComponent { }
##about.component
import { Component } from '@angular/core'; import { ActivatedRoute, ROUTER_DIRECTIVES } from '@angular/router'; @Component({ selector: 'about-home', template: `<h3>user</h3>` }) export class AboutHomeComponent { } @Component({ selector: 'about-item', template: `<h3>product</h3>` }) export class AboutItemComponent { } @Component({ selector: 'app-about', templateUrl: '../app/layouts/admin.html', directives: [ROUTER_DIRECTIVES] }) export class AboutComponent { }
This generally occurs when there is a mismatch in the routes specified, or a component which is not present in our routing configuration, or if there is the use of multiple routes in our angular application which is not properly configured.
You need to export the RouterModule from there so that it is available to your AppModule when it imports it. To fix that, export it from your AppRoutingModule by adding it to the exports array.
Instead of “href” attribute of anchor tag, we use the “routerLink” attribute of Angular. The routerLink attribute allows us to link to a specific route of the Application.
Generate an application with routing enabledlink The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is an NgModule where you can configure your routes.
I think that your mistake is in that your route should be product
instead of /product
.
So more something like
children: [ { path: '', component: AboutHomeComponent }, { path: 'product', component: AboutItemComponent } ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With