Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Uncaught (in promise): Error: Cannot match any routes Angular 2

##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'

enter image description here

After Click Product it shows this enter image description here

##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 { }        
like image 410
Malik Kashmiri Avatar asked Jun 29 '16 07:06

Malik Kashmiri


People also ask

Can not match any routes Angular?

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.

How do you handle Cannot match any routes?

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.

What would you use in Angular 2 to define route?

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.

Which Angular package is used to route to URL?

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.


1 Answers

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     }  ] 
like image 99
Dylan Meeus Avatar answered Sep 29 '22 22:09

Dylan Meeus