I am using Angular 2.1.0 in a Visual Studio 2015 AspNetCore project and having an issue with routing in my lazy loaded modules. The error is as below:
core.umd.js:3257 EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'Home/MyList' Error: Cannot match any routes. URL Segment: 'Home/MyList' at ApplyRedirects.noMatchError (http://localhost:31534/libraries/@angular/router/bundles/router.umd.js:769:20) at CatchSubscriber.eval [as selector] (http://localhost:31534/libraries/@angular/router/bundles/router.umd.js:747:33) at CatchSubscriber.error (http://localhost:31534/libraries/rxjs/operator/catch.js:52:31) at MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) at MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) at MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) at MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) at MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) at MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) at LastSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26)
I have followed the Tour Of Heroes tutorial and adapted it according to the lazy load topic here
I am loading a login page first {Home/Login}
in the Bootstrapped module called AppModule
. On clicking login I want to show the next page MyList
, {Home/MyList}
which is lazily loaded into a feature module called MyModule
.
MyModule
is based on theHeroModule
from the second link above.
MyList
page and related component is the first page/component under MyModule
which also has a MyListDetail
page/component {Home/MyListDetail}
.
I navigate to {Home/MyList}
on a button click using this code
let link = ['Home/MyList'];
this.router.navigate(link);
Why are my routes not being found? Here is how I have them setup.
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
export const routes: Routes = [
{ path: '', redirectTo: 'Home/Login', pathMatch: 'full'},
{ path: 'Home/MyList', loadChildren: 'scripts/My.Module#MyModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
login-routing.module.ts
import { NgModule } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
import { LoginCmpnt } from './Login';
@NgModule({
imports: [RouterModule.forChild([
{ path: 'Home/Login', component: LoginCmpnt}
])],
exports: [RouterModule]
})
export class LoginRoutingModule {}
MyList-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes } from '@angular/router';
import { RouterModule } from '@angular/router';
import { SharedModule } from './shared.module';
import { MyComponent } from './MyComponent';
import { MyListComponent } from './MyListComp';
import { MyListDetailComponent } from './MyListDetailComp';
const routes: Routes = [
{ path: '',
component: MyComponent,
children: [{ path: 'Home/MyList', component: MyListComponent },
{ path: 'Home/MyListDetail/:id', component: MyListDetailComponent }]
}];
@NgModule({
imports: [RouterModule.forChild([routes]), SharedModule ],
exports: [RouterModule]})
export class MyListRoutingModule {}
In the MyList-routing.module
above what should I put for the path against MyComponent
? The login method routes to Home/MyList
EDIT
Even this with the slash preceding the path in the login click doesn't work.
let link = ['/Home/MyList'];
this.router.navigate(link);
let link = ['/Home/MyList'];
(added/
)? – Günter Zöchbauer