我的应用程序利用Angular Universal,我想在Angular组件中获取当前url
的完整路径。我认为我必须使用window
对象,在那里我可能需要注入窗口,或者更好的选择是简单地检查它是否在浏览器中。有没有更干净的解决方案利用Angular的API?
url: string;
constructor(private router: Router) { }
ngOnInit() {
this.router.events.subscribe((val) => {
this.url = `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
});
}
编辑:我要问的问题是如何以符合Angular Universal标准的方式获取完整的url
。