我使用用户名和密码admin&admin进行正常登录 . 在登录组件导航到layout.But我收到一个错误,如“core.js:1448 ERROR错误:未捕获(在承诺中):错误:无法匹配任何路由 . 网址细分:'布局'“ . 请帮帮我..
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 { }
1 回答
扩展JB Nizet在你的评论中所说的,当你有一个延迟加载的模块时,那个延迟加载的模块里面的路径是相对的 . 如果在延迟加载之前有任何路径,则该路径预先设置为延迟加载模块中的所有路径 .
所以在这种情况下,这是您的路径映射的方式:
路径:''
加载的组件:LoginComponent
原因:重定向到'/login' . 请参阅下面的登录以获取更多信
路径:'/login'
加载的组件:LoginComponent
原因:延迟加载LoginModule,其中匹配''的路径,从而加载LoginComponent
路径:'/login/layout'
已加载的组件:LayoutComponent
原因:延迟加载LoginModule,其中匹配'layout'的路径,从而加载LayoutComponent
路径:'/layout'
加载的组件:无
原因:因为这不是'login'的前缀,它不会延迟加载LoginModule,因此只是在app.routing中查找,其中唯一有效的路径是'' and ' / login'