要解决“Angular: 使用RouterLinkActive的子路由不起作用”的问题,可以尝试以下解决方法:
RouterModule
和RouterModule.forRoot()
方法,并在子模块中导入RouterModule.forChild()
方法。import { RouterModule } from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot(routes),
// other imports
],
// other configurations
})
export class AppModule { }
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild(routes),
// other imports
],
// other configurations
})
export class ChildModule { }
RouterLinkActive
指令添加routerLinkActiveOptions
属性,并将{exact: true}
传递给它。这将确保只有在精确匹配时才会激活链接。
children
属性来定义子路由。const routes: Routes = [
{ path: 'parent', component: ParentComponent, children: [
{ path: 'child', component: ChildComponent }
]},
];
请注意,这些解决方法可能因应用程序的特定情况而有所不同。确保按照你的应用程序的要求进行相应的调整。