如何在Angular 4中获取路由之前的URL

问题描述 投票:0回答:1

假设我有两条路线:

{ path: '/path1', component: component1 }
{ path: '/path2', component: component2 }

我在'/ path1'并导航到'/ path2'。

所以我的问题是如何在组件2构造函数或代码中的任何位置获取路由url'/ path1'而不通过任何服务使用resolve或global变量。

Component2.ts

export class Component2{
    constructor(router: Router) {
        router.events.subscribe((event) => {
            // here I want to get route url '/path1' from component1
        });
    }
}

如果有人需要更多澄清,请告诉我。

angular angular2-routing angular4-router
1个回答
0
投票

我不确定你想怎么用它。但是如果你知道应用程序中的可用路径,你可以简单地在component2中使用:this.router.navigate([“/ path1”]);你可以使用的基本URL,例如:location.host所以你的网址是location.host +“/ path2”这就是你要找的?

© www.soinside.com 2019 - 2024. All rights reserved.