【问题标题】:Get root url with subdirectories in Angular在 Angular 中获取带有子目录的根 url
【发布时间】:2020-10-27 12:31:04
【问题描述】:

所以我有这个应用程序,上面安装了多个库,每个库都有自己的 api。我想检索每个库的 url,这些库看起来像 http://hostname.com/subdir1/subdir2/exampleRoute 而没有路由('exampleRoute'),因为 subdir 2 是我的根文件夹,并在它之后添加 /libraryname,所以它看起来像 http://hostname.com/subdir1/subdir2/libraryname。子目录的数量不固定,可能更多,所以如果我有 3 个子目录,我的根 url 将是http://hostname.com/subdir1/subdir2/subdir3。 我曾经在路由的根组件中使用window.location.href 执行此操作,并手动切断最后一段,但这不是最好的解决方案,因为我必须能够在确实有路由的组件中获取这些 url。

【问题讨论】:

  • 您是否尝试过使用ActivatedRoute/ActivatedRouteSnapshot.url?
  • 我没有,是否可以使用该或仅当前路由状态 url 获取根路由 url?例如。托管在hostname.com/subdir1/subdir2/myapp/randomRoute,我可以在任何路由器状态下获得“hostname.com/subdir1/subdir2/myapp”部分吗?
  • 是的,检索路由 url 应该没有问题,但是带有 hostname.com 的部分通常作为 baseUrl 变量添加到主 app.config.ts 文件中,我建议检查 ActivatedRoute和 ActivatedRouteSnapshot 属性,看看那里的任何东西是否符合您的需求。
  • 你可以使用path.basename(path.dirname(filename))

标签: javascript angular iis


【解决方案1】:

您可以轻松使用 document.head.baseURI 得到你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多