【发布时间】:2022-06-25 03:26:12
【问题描述】:
在我的网站上工作,在实例化 root 服务时必须向 api 发出一些请求。
我想根据url获取一些数据,所以我做了以下
@Injectable({
providedIn: 'root',
})
export class MyService {
constructor(private _router: Router) {
this.init()
}
init() {
console.log('url', this._router.url, window.location.href)
// this._router.url : `/`
// window.location.href : `http://localhost:4201/my/url`
// Here I'll make some get request with the url
}
}
但我发现路由器还没有正确的值,或者至少不是一直都没有。
有趣的是,有时它确实有效。
我应该怎么做才能在不使用window 对象的情况下获取位置?
【问题讨论】:
标签: javascript angular typescript angular-routing