我有一个非常简单的项目来测试嵌套路由。错误说,应用程序路由有效,但子进程“无法匹配任何路由”。检查了其他答案,但版本使我感到困惑。这是我所拥有的:
APP.MODULE.TS
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CountriesModule } from './countries/countries/countries.module';
import { AppRoutingModule } from './app-routing.module';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { AdressComponent} from './adress/adress.component';
@NgModule({
declarations: [AppComponent, AdressComponent],
imports: [BrowserModule, AppRoutingModule, CountriesModule, RouterModule ],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
App.Routing,Module.TS
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AdressComponent } from './adress/adress.component';
import { CountriesComponent } from './countries/countries/countries.component';
export const routes: Routes = [
{
path: '',
component: AdressComponent
},
{
path: 'country',
component: CountriesComponent
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }
Countries.Module(嵌套)
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CountriesComponent } from './countries.component';
import { AddCountryComponent } from './add-country/add-country.component';
import { CountryListComponentComponent } from './country-list-component/country-list-component.component';
import { RouterModule } from '@angular/router';
@NgModule({
declarations: [
CountriesComponent,
AddCountryComponent,
CountryListComponentComponent
],
imports: [CommonModule, RouterModule]
})
export class CountriesModule {}
路由:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CountriesComponent } from './countries.component';
import { AddCountryComponent } from './add-country/add-country.component';
import { CountryListComponentComponent } from './country-list-component/country-list-component.component';
export const countryRoutes: Routes = [
{
path: 'country',
component: CountriesComponent,
children: [
{
path: 'add',
component: AddCountryComponent
},
{
path: 'list',
component: CountryListComponentComponent
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(countryRoutes)],
exports: [RouterModule]
})
export class CountryRoutingModule {}
HTML(国家/地区)
<a [routerLink]="['/country/list/']" routerLinkActive="active">List</a>
编辑:@Maihan Nijat
The project in StackBlitz: https://stackblitz.com/edit/angular-rsj8qd
答案 0 :(得分:0)
应该只是 list
,从routerLink中删除最后一个反斜杠
<a [routerLink]="['/country/list']
答案 1 :(得分:0)
应用程序路由模块:
编辑路径option allbackups
以匹配您的路径。在测试之前,请遍历代码中的路径。
app/countries.module#CountriesModule'
国家路由:
const routes: Routes = [
{path: 'country', loadChildren: 'app/countries/countries/countries.module#CountriesModule'},
{path: '', component: AdressComponent},
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
国家模块:
const routes: Routes = [
{
path: '',
component: CountriesComponent,
children: [
{
path: 'add',
component: AddCountryComponent
},
{
path: 'list',
component: CountryListComponentComponent
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CountryRoutingModule {}
export const routedComponents = [
CountriesComponent,
CountryListComponentComponent,
AddCountryComponent,
];