【发布时间】:2018-04-19 08:20:01
【问题描述】:
我的应用程序利用了 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
【问题讨论】:
-
试试这样:console.log("location", window.location.href);
-
编辑澄清问题