我当前的网址是:
https://localhost:8080/MyApp/manager/dashboard
在上面的 url 中,我只想获取 https://localhost:8080/MyApp/部分。
有什么方法可以在不硬编码的情况下获取它吗?
最佳答案
window.location 包含很多信息。假设网址是 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"
关于javascript - Angular 5 : Get host name and app name from URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49297680/