在服务中如何访问为角度2/4中的特定路线添加的静态数据? [英] How to access static data added for particular route in angular 2/4 in service?
本文介绍了在服务中如何访问为角度2/4中的特定路线添加的静态数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
const routes: Routes = [
{
path: 'somepath',
component: SomeComponent,
data: {
showSidebar: true,
title: 'test'
}
},
{
path: 'somepath2',
component: SomeComponent2,
data: {
showSidebar: false,
title: 'test2'
}
}
];
如何从当前激活的路径访问数据对象? 如果我当前的路由是localhost:4200/app/somepath2,那么我想访问 数据:{showSidebar:否,标题:'test2'}
How can I access data object from currently activates route? If my current route is localhost:4200/app/somepath2 then I want to access data: { showSidebar: false, title: 'test2' }
推荐答案
您可以使用 this.route.snapshot.data
You could use this.route.snapshot.data
在构造函数中注入路由器
Inject router in the constructor
import { ActivatedRoute } from '@angular/router';
constructor(private route:ActivatedRoute)
ngOnInit() {
const myData = this.route.snapshot.data;
}
这篇关于在服务中如何访问为角度2/4中的特定路线添加的静态数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文