What's the difference between --base-href and

2020-01-29 04:16发布

问题:

The documentation of Angular informs I should use --base-href parameter in the Angular application build for production when it's going to be deployed in a subfolder of server:

If you copy the files into a server sub-folder, append the build flag, --base-href and set the appropriately.

For example, if the index.html is on the server at /my/app/index.html, set the base href to like this.

https://angular.io/guide/deployment

However, the angular-cli has the --deploy-url parameter. The documentation of tool describes it as:

URL where files will be deployed.

https://github.com/angular/angular-cli/wiki/build

I have seen solutions that use the --deploy-url insted of --base-href when the application is going to be deployed in a subfolder of server.

The question

What's the difference between --base-href and --deploy-url parameters of angular-cli tool? When should I use each one?

回答1:

  • Base-href is being used by routing
  • deploy-url is for assets.

In most cases base-href is enough.

Please see these posts:

  • https://github.com/angular/angular-cli/issues/9835

  • https://shekhargulati.com/2017/07/06/angular-4-use-of-base-href-and-deploy-url-build-options/



回答2:

To put my scripts inside the "/test/app1/script/" folder, I use this command:

ng build --prod --base-href /test/app1/ --deploy-url /test/app1/script/

Thus my app is accessible at https://example.com/test/app1/ but my JS scripts and CSS are in the https://example.com/test/app1/script/ directory.



回答3:

If I want to use /users as my application base for the router and /public as a base for my assets.

ng build --prod --base-href /users --deploy-url /public 

See Shekhar Gulati's blog for a detailed example...