Angular2 - retrieve current url

后端 未结 1 461
傲寒
傲寒 2021-01-24 10:15

I just updated to Angular 2.0.0-rc5 and my listener for the current page url seems to have been deprecated.

Previously I could access it by subscribing to the following:

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 10:51

    Look for router.events and subscribe to that. The typical event will be NavigationEnd and this will give you the url information that you need.

    router.events.subscribe(x => {
                if(x instanceof NavigationEnd) {
                    console.log(x.url);
                    console.log(x.urlAfterRedirects);
                }
            });
    

    Note that there are other kind of events too, but the basic scenario uses NavigationEnd.

    0 讨论(0)
提交回复
热议问题