更改 ng serve 中的基本 href(Angular 15)

huangapple go评论67阅读模式
英文:

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 ("&lt;base href=&#39;/&#39; /&gt;") 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

  &lt;script&gt;
    var items = document.location.href.split(&#39;/&#39;);
    while (items.length&gt;4)
      items.pop();
    document.write(&#39;&lt;base href=&quot;&#39; + items.join(&#39;/&#39;) + &#39;&quot; /&gt;&#39;);
  &lt;/script&gt;

答案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

{
...
&quot;configurations&quot;:{
   &quot;site1&quot;:{
      &quot;fileReplacements&quot;:[
         {
            &quot;replace&quot;:&quot;src/index.html&quot;,
            &quot;with&quot;:&quot;src/environments/environment.site1.index.html&quot;
         }
      ]
   }
}
...
}

then

ng serve --serve-path site1 configuration=site1

huangapple
  • 本文由 发表于 2023年6月8日 17:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定