英文:
Change base href with ng serve (Angular 15)
问题
我有一个在Angular 15中的应用程序,我希望能够同时在两个或更多不同的路径上启动它,例如在"http://localhost:4200/site1"和"http://localhost:4200/site2"(客户端不希望使用子域或更改端口,而且我希望在每次更改代码时都能够进行测试,而不必部署到开发服务器)。
我发现了--serve-path选项,所以如果我执行"ng serve --serve-path site1"和"ng serve --serve-path site2",路径可以工作,但由于index.html中的基本标签("<base href='/' />
"),我无法获取内容,而且我不能更改它,因为我需要同时打开两个站点。
我发现有些人说在先前版本中ng serve有两个用于此目的的选项("--base-href"和"--deploy-url"),但在Angular 15中它们不存在。
如何在Angular 15中实现这个目标?
谢谢。
英文:
I have an application in Angular 15 and I want to be able to start it in two or more different paths at the same time, for example in "http://localhost:4200/site1" and "http://localhost:4200/site2" (having subdomains or changing port is not an option for the client and I want to test this without having to deploy to a development server each time we change code).
I have found the option --serve-path so if I do "ng serve --serve-path site1" and "ng serve --serve-path site2" the path works but I don't get the contents because of the base tag in index.html ("<base href='/' />
") and I can't change it because I need to have the two sites open at same time.
I have found that some people say that in previous versions ng serve had two options for that ("--base-href", and "--deploy-url"), but in Angular 15 they don't exist.
How can I get this in Angular 15?
Thanks.
答案1
得分: 1
From this old SO -really I don't know if work-
try use
<script>
var items = document.location.href.split('/');
while (items.length > 4)
items.pop();
document.write('<base href="' + items.join('/') + '" />');
</script>
英文:
From this old SO -really I don't know if work-
try use
<script>
var items = document.location.href.split('/');
while (items.length>4)
items.pop();
document.write('<base href="' + items.join('/') + '" />');
</script>
答案2
得分: 0
在angular.json中,您可以创建两个不同的配置,并在它们中更改index.html。
{
...
"configurations":{
"site1":{
"fileReplacements":[
{
"replace":"src/index.html",
"with":"src/environments/environment.site1.index.html"
}
]
}
}
...
}
然后执行以下命令:
ng serve --serve-path site1 configuration=site1
英文:
You could create two different configurations and change the index.html in them
in angular.json
{
...
"configurations":{
"site1":{
"fileReplacements":[
{
"replace":"src/index.html",
"with":"src/environments/environment.site1.index.html"
}
]
}
}
...
}
then
ng serve --serve-path site1 configuration=site1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论