Suv*_*kar 11 javascript typescript angular angular5
我目前的网址是:
https://localhost:8080/MyApp/manager/dashboard
在上面的 url 中,我只想获取https://localhost:8080/MyApp/部分。
有没有办法在没有硬编码的情况下获取它?
Vik*_*ari 15
window.location包含大量信息。假设 URL 是 https://example.com:80/a/b/c,您可以在 Angular 中轻松获取以下信息:
window.location.hostname = "example.com"
window.location.host = "example.com:80"
window.location.protocol = "https:"
window.location.port = "80"
window.location.pathname = "/a/b/c"
window.location.href = "https://example.com:80/a/b/c"
Run Code Online (Sandbox Code Playgroud)
小智 8
如果您只想获取域名和应用程序名称,则依赖路由器位置和窗口位置。
import { Location } from '@angular/common';
constructor(private loc: Location) {}
ngOnInit() {
const angularRoute = this.loc.path();
const url = window.location.href;
const domainAndApp = url.replace(angularRoute, '');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19478 次 |
| 最近记录: |