Angular 4中是否有可以调用的内容,我传入字符串或路由令牌数组并获取该路由的静态路由数据?
例如:
const targetRoute = '/test/route'
const routeData = {{something}}.getRouteData(targetRoute)
// { routeDataValue: 'something' } etc...
我要查找的数据是路线定义
中定义的数据 const routes: Routes = [
{
path: 'test/route',
data: { //This data object
animation: {
value: 'fetching-results'
},
progress: 100,
sectionIdentifier: {
background: 'results',
backLinkUrl: null,
backLinkText: null
}
},
}
]
答案 0 :(得分:4)
您可以从路由器获取所有路由和路由配置(包括数据属性),如下所示:
import { Router } from '@angular/router';
...
constructor(
private router: Router) {
}
ngOnInit() {
// all routes
console.log(this.router.config);
// data of test/route
console.log(this.router.config.find(route => route.path === 'test/route').data);
}