【问题标题】:Obtain Route information given any url in Angular 10获取 Angular 10 中任意 url 的路由信息
【发布时间】:2020-07-20 20:23:33
【问题描述】:

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

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

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

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

【问题讨论】:

标签: angular angular-router angular10


【解决方案1】:

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

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

   ngOnInit() { 
   this.activatedroute.data.subscribe(data => {
       this.product=data;
   }) }

【讨论】:

    【解决方案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.
    }
    

    【讨论】:

    • 这并不能解决我的问题,我已经用更多细节更新了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 2015-12-24
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多