如何检测角度2中的URL更改?

Huy*_*ham 5 javascript angular2-routing angular

可以使用router.events流检测路由更改.(如何检测Angular 2中的路径变化?).

但我不确定如何检测浏览器URL更改.

Gün*_*uer 3

您可以注入Location并订阅它

import { Location } from '@angular/common';

...

constructor(location:Location) {
  location.subscribe(val => console.log(val));
}
Run Code Online (Sandbox Code Playgroud)

正如哈利提到的。这只通知 popState 事件(路由器或类似代码更改 URL)