router.url 页面更改时不会更改其内容
router.url does not change it's content when page is changed
我想检索我的 angular 应用程序的当前 url 并执行一些任务。我希望包含 url 的变量在我更改页面时发生变化。
我写了:
export class SidenavComponent implements OnInit {
public href: string = "";
}
ngOnInit() {
this.href = this.router.url;
console.log(this.href);
}
问题是 this.href
没有改变它的内容。假设我在首页,那么this.href
就是"dashboard/home"
。现在我点击一个按钮并转到另一个页面,然后 url 变成 "localhost:4200/dashboard/target"
但 this.href
值没有改变
监听路由器事件以更新它。
this.router.events.subscribe(() => this.href = this.router.url);
我想检索我的 angular 应用程序的当前 url 并执行一些任务。我希望包含 url 的变量在我更改页面时发生变化。
我写了:
export class SidenavComponent implements OnInit {
public href: string = "";
}
ngOnInit() {
this.href = this.router.url;
console.log(this.href);
}
问题是 this.href
没有改变它的内容。假设我在首页,那么this.href
就是"dashboard/home"
。现在我点击一个按钮并转到另一个页面,然后 url 变成 "localhost:4200/dashboard/target"
但 this.href
值没有改变
监听路由器事件以更新它。
this.router.events.subscribe(() => this.href = this.router.url);