在Angular中,嵌套路由是通过创建子路由来实现的。要设置嵌套路由出口的URL模式,可以按照以下步骤进行操作:
const routes: Routes = [
{ path: 'parent', component: ParentComponent, children: [
{ path: 'child', component: ChildComponent, outlet: 'childOutlet' }
]}
];
router-outlet
指令来定义子路由的出口:
Router.navigate
方法:import { Router } from '@angular/router';
...
constructor(private router: Router) {}
navigateToChild() {
this.router.navigate([{ outlets: { childOutlet: 'child' }}]);
}
这样,当点击按钮时,将会导航到/parent(childOutlet:child)
的URL,并在childOutlet
出口中渲染ChildComponent
。
希望以上解决方法能对你有所帮助!