javascript - Angular 4中的动态嵌套路由器插座使用
问题描述
Angular 4中的动态嵌套路由器插座使用
我有简单的 Angular CLI,带有 angular 的动态路由。
在我的应用程序中,我有 2 个不同的页面,例如 home 和 about。在那个主页中,我想插入更多具有动态内容的主题。所以我在主页中包含了嵌套路由器。在导航嵌套内容时,它会替换为根元素。我已附上在线示例,请检查。谁能为此提供解决方案。
路由器配置
import { Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { HomeNestComponent } from './home/homenest/homenest.component';
export const rootRouterConfig: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'home/homenest', component: HomeNestComponent },
];
解决方案
在路由器配置中进行以下更改。
import { Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { HomeNestComponent } from './home/homenest/homenest.component';
export const rootRouterConfig: Routes = [
{ path: 'home', component: HomeComponent,
children:[{ path: 'homenest', component: HomeNestComponent }]
},
{ path: 'about', component: AboutComponent },
{ path: 'homenest', component: HomeNestComponent },
];
home.component.ts 的变化。
public homeNestClick(e) {
this.router.navigateByUrl('/home/homenest');
}
https://stackblitz.com/edit/angular-tf7ru3?file=src/app/home/home.component.ts
推荐阅读
- python - 如何使用 numpy 处理时间敏感数据?
- jwt - JWT 令牌和刷新令牌有什么区别?
- php - 检查 Internet 访问的更好和更简单的方法。PHP 定时任务
- python - 在 Windows 中运行 Flask 看不到环境变量
- firefox-addon-webextensions - 如何侦听 Web 应用程序本身启动的 Web 扩展程序中的 URL 更改?
- vb.net - vb.net 表单不会启动它只是关闭
- php - 将会话变量插入 MySQL
- r - 在 R 中替换矩阵中的 NA
- vue.js - 如何基于 vuex 状态模糊或聚焦 vuejs 组件
- android - Github GraphQl 获取 REPOSITORY 订阅者数量和订阅者列表