【发布时间】:2018-04-02 16:15:40
【问题描述】:
我正在使用用户名和密码 admin 和 admin 进行正常登录。在登录组件中导航到布局。但我收到类似“core.js:1448 ERROR Error: Uncaught (in promise): Error: Cannot match”之类的错误任何路线。URL 段:'layout'"。请帮助我..
app.routing.module.ts
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
{path: '', redirectTo: '/login', pathMatch: 'full'},
{path: 'login', loadChildren: 'app/login/login.module#LoginModule'}
])
],
exports: [
RouterModule
]
})
export class AppRoutingModule {
}
我的 login.component.ts 是
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthenticationService } from '../../services/authentication.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
username:string;
password:string;
constructor(
public authService: AuthenticationService,
private route: ActivatedRoute,
private router: Router) { }
ngOnInit() {
}
login(){
if(this.authService.login(this.username, this.password)){
this.router.navigate(['/layout']);
}
}
}
我的 login.routing.module.ts 是
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { LayoutComponent } from '../layout/layout/layout.component';
import { LayoutRoutingModule } from '../layout/layout-routing.module';
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'layout', component: LayoutComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class LoginRoutingModule { }
【问题讨论】:
-
您正在导航到布局路线但尚未定义。
-
布局路由是登录路由的子路由。它的路径是/login/layout。