javascript - Angular 5 : Get host name and app name from URL

标签 javascript angular typescript angular5

我当前的网址是:

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/

相关文章:

javascript - React 句柄更改功能无需参数事件即可工作

javascript - 需要 HTML5 Localstorage 帮助

javascript - 启用了 CORS 但仍然得到 405

angular - Angular 为 4 的 html2canvas

angular - App_Initializer 不适用于 UI 路由器

typescript - Visual Studio 代码 :[js] types can only be used in a . ts 文件

javascript - 即使我有链接,如何强制打开我的 jQuery 对话框?

javascript - AJAX无法加载URL

Angular2 Routing - 在路由更改时保持组件状态

routing - Angular - 路由参数