我设置的路线如下
const appRoutes: Routes = [
{
path: 'login',
component: LoginComponent,
data: {
title: 'Login TTX'
}
},
{
path: 'list',
component: ListingComponent,
data: {
title: ' TTX Home Page',
module:'list'
}
},
{
path: '',
redirectTo: '/login',
pathMatch: 'full'
},
];
Run Code Online (Sandbox Code Playgroud)
现在,当我来'/list' 路线然后'listing.component.ts'我写下面的代码
export class ListingComponent {
public constructor(private router:Router) {
//here how i can get **data** of **list** routes
}
}
Run Code Online (Sandbox Code Playgroud) 因此,我需要以某种方式检查我是否在主页上并做某事,而在其他页面中则不这样做.还在所有页面上导入该组件.
如果我在主页上,如何检测该组件?
谢谢
const routes: Routes = [
{
path: 'somepath',
component: SomeComponent,
data: {
showSidebar: true,
title: 'test'
}
},
{
path: 'somepath2',
component: SomeComponent2,
data: {
showSidebar: false,
title: 'test2'
}
}
];
Run Code Online (Sandbox Code Playgroud)
如何从当前激活的路线访问数据对象?如果我当前的路由是 localhost:4200/app/somepath2 那么我想访问数据: { showSidebar: false, title: 'test2' }
我配置了以下路由 app-routing.module.ts
const routes: Routes = [
{
path: 'abc/:id', component: AbcComponent, data: { category: 'Public' }
},
{
path: 'xyz/:id/tester/:mapId', component: XyzComponent, data: { category: 'Private' }
},
{ path: '**', redirectTo: '/page-not-found', pathMatch: 'full'}
]
Run Code Online (Sandbox Code Playgroud)
在app.component.ts我想根据传递的 URL 获取每个路由的类别:
例如:go to http://myapp.com/abc/123 should return category as Public
going to http://myapp.com/xyz/123/tester/456 should return category asPrivate
这是我到目前为止所拥有的:
constructor(
private activatedRoute: ActivatedRoute,
private router: Router
)
{
checkRouteAndGetCategory()
}
checkRouteAndGetCategory()
{
this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(() => …Run Code Online (Sandbox Code Playgroud)