简体   繁体   English

在 Angular 14 中为 ng 服务覆盖 baseHref

[英]override baseHref for ng serve in Angular 14

I have a set "baseHref" to '/bui' in the angular.json file because the static files are deployed there.我在 angular.json 文件中将“baseHref”设置为“/bui”,因为静态文件部署在那里。 If I use ng serve with port 4200 and I navigate to https://localhost:4200 I get the following message: Cannot GET /如果我使用端口 4200 的 ng serve 并导航到 https://localhost:4200,我会收到以下消息:无法获取 /

This is due to the baseHref.这是由于baseHref。 In the past we would use a base href set to '/' and the deployUrl set to '/bui/'.过去我们会使用设置为'/'的基本href和设置为'/bui/'的deployUrl。 The deployUrl is now deprecated (Angular 14) so we have to use base href. deployUrl 现在已弃用(Angular 14),因此我们必须使用基本 href。

In production mode on the server the base href is /something-extra/bui/.在服务器的生产模式下,基本 href 是 /something-extra/bui/。 We also want to use the application with base href /bui/ in case we test the application with aspnet core and the static files (wwwroot).我们还希望使用带有基本 href /bui/ 的应用程序,以防我们使用 aspnet 核心和静态文件 (wwwroot) 测试应用程序。

I cannot seem to override the baseHref within the ng-serve section of the angular.json file.我似乎无法覆盖 angular.json 文件的 ng-serve 部分中的 baseHref。 How can I override the base href in ng serve modus to '/'?如何将ng服务模式中的基本href覆盖为'/'?

angular.json角.json

.
.
.
 "configurations": {
            "development": {
              "baseHref": "/",
.
.
.
"production": {
              "baseHref": "/bui/",

index.html索引.html

.
.
.
<base href="/">
...

So what i did is In angular json build: "baseHref": "/所以我所做的是 In angular json build: "baseHref": "/

In angular json build production section: "baseHref: '/something-extra/bui/'在 Angular json 构建生产部分: “baseHref: '/something-extra/bui/'

In the ng build for testing with wwwroot aspnet core: ng build --base-href /bui/在使用 wwwroot aspnet core 进行测试的 ng build 中: ng build --base-href /bui/

This way the ng serve root url always gives a result instead of not found which was the original problem.这样,ng serve root url 总是给出结果,而不是原始问题的 not found。 And it also works in localhost outside webpack dev server with physical files.它也可以在带有物理文件的 webpack 开发服务器之外的 localhost 中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM