我在导航到延迟加载模块中定义的组件时遇到问题。该组件ListComponent
在路径“test”下定义(my-routing.module.ts - 延迟加载模块)。每当我单击“测试”按钮(test.component.html)时,我都会收到异常:
NG04002:无法匹配任何路由。URL 段:“测试”。
为什么?
我的app-routing.module.ts如下所示:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
import { AboutComponent } from './components/about/about.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { AuthGuard } from './auth.gard';
import { ListComponent } from './list.component';
import { TestComponent } from './test/test.component';
const myModule = () => import('./my-routing/my-.module').then(x => x.MyRoutingModule);
const routes: Routes = [
{ path: '', component: TestComponent },
{ path: 'experiment', loadChildren: myModule },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
我的my-routing.module.ts是:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ListComponent } from '../list.component';
import { TestComponent } from '../test/test.component';
const routes: Routes = [
{ path: 'test', component: ListComponent },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class MyRoutingModule { }
我的test.component.html看起来像这样:
<div class="container">
<div class="row">
<div class="col-1">
<a [routerLink]="['test']"><button type="button" class="btn btn-primary">
Test</button></a>
</div>
</div>
</div>
如果您阅读了app.routing.ts,
中的所有路由都
MyRoutingModule
将以路径:“experiment/”开头。因此,将路径“测试”更改为“实验/测试”。
请注意,您可以将该
[routerLink]
属性应用于该<button>
元素,而无需该<a>
元素。演示@StackBlitz