Angular4、获取路由数据而不实际路由
Angular 4, get route data without actually routing
Angular4 中有什么我可以调用的,我可以在其中传入字符串或路由标记数组并取回该路由的静态路由数据吗?
例如:
const targetRoute = '/test/route'
const routeData = {{something}}.getRouteData(targetRoute)
// { routeDataValue: 'something' } etc...
我要查找的数据是Route Definitions中定义的数据
const routes: Routes = [
{
path: 'test/route',
data: { //This data object
animation: {
value: 'fetching-results'
},
progress: 100,
sectionIdentifier: {
background: 'results',
backLinkUrl: null,
backLinkText: null
}
},
}
]
您可以像这样从路由器获取所有路由和路由配置(包括数据 属性):
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);
}
Angular4 中有什么我可以调用的,我可以在其中传入字符串或路由标记数组并取回该路由的静态路由数据吗?
例如:
const targetRoute = '/test/route'
const routeData = {{something}}.getRouteData(targetRoute)
// { routeDataValue: 'something' } etc...
我要查找的数据是Route Definitions中定义的数据
const routes: Routes = [
{
path: 'test/route',
data: { //This data object
animation: {
value: 'fetching-results'
},
progress: 100,
sectionIdentifier: {
background: 'results',
backLinkUrl: null,
backLinkText: null
}
},
}
]
您可以像这样从路由器获取所有路由和路由配置(包括数据 属性):
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);
}