1

在 Angular 10 中,我的路线中有以下条目:

const routes = [
  { path:'my/target/:targetId', component: MyTargetComponent, data: { dataKey: 'dataValue} },
   ...
]

从我的应用程序内部的任何地方,给定任何路径(例如my/target/55),我需要能够访问包含在 Route 对象中的所有信息,这些信息用于定义此类路由(特别是我需要读取该data字段)。Angular 10 中是否有内置函数可以做到这一点?

更新:我需要访问不一定是当前页面 url 的 url 信息。

4

2 回答 2

-2

像这样试试

constructor(private route: Router) {}
ngOnInit() {
    console.log(this.router);
    // on console expand this object and will get all details of routes of complete application in **this.router.config** object.
}
于 2020-07-20T10:39:13.337 回答
-2

Angular 路由器将传递动态数据。假设,当 MyTargetComponent 被渲染时,data:{ id:'1', name:"Angular10"}。数据值将位于 ActivatedRoute 服务的数据属性中

然后我们可以通过订阅激活的route.data 属性来读取数据,如下所示:

   ngOnInit() { 
   this.activatedroute.data.subscribe(data => {
       this.product=data;
   }) }
于 2020-07-20T11:08:11.053 回答