【发布时间】:2016-11-01 22:42:32
【问题描述】:
我们有一个为客户端使用 Angular 2 的企业应用程序。我们的每个客户都有自己独特的网址,例如:https://our.app.com/customer-one 和 https://our.app.com/customer-two。目前我们可以使用document.location动态设置<base href...>。所以用户点击https://our.app.com/customer-two 和<base href...> 被设置为/customer-two...完美!
问题是,例如,如果用户位于 https://our.app.com/customer-two/another-page 并且他们刷新页面或尝试直接点击该 url,则 <base href...> 被设置为 /customer-two/another-page 并且找不到资源。
我们尝试了以下方法均无济于事:
<!doctype html>
<html>
<head>
<script type='text/javascript'>
var base = document.location.pathname.split('/')[1];
document.write('<base href="/' + base + '" />');
</script>
...
不幸的是,我们还没有新的想法。任何帮助都会很棒。
【问题讨论】:
-
你有没有尝试过使用APP_BASE_HREF?我不太确定它将如何实现,但它似乎是有用的......
-
当然这也取决于您当前使用的路由器版本。你在路由器版本 3.0.0-alpha.x 上吗?
-
我正在使用“@angular/router”:“3.0.0-alpha.7”
-
Angular 7 - 2018 年 11 月。这仍然是一个问题。当前的答案提供了部分解决方案。在 Angular 模块中做事为时已晚。当您的索引 HTML 被提供时,它需要知道从哪里提取脚本。我看到的方式只有 2 种方式 - 使用 asp\php\whatever 为 index.html 提供动态 basehref 内容。或在 index.html 文件上使用本机 javascript 在 DOM 内容变为 Angular 之前对其进行更改。两者都是对一个共同问题的糟糕解决方案。伤心。
-
有人也可以回答我的问题吗? stackoverflow.com/questions/53757931/…
标签: angular