问题
I have a small project written in Angular 7 and im trying to build the production version out of it.
When i run
ng build --prod --aot --service-worker
The build will run without errors as seen on screenshot.
On the other hand, when im trying to load the app in the browser, it will throw errors in console and will not load at all.
Any ideas what is causing this? The files are in the same directory as the index.html and they do exist.
回答1:
So, I figured out that if I want to serve this files statically without using some sort of server in front of the page, I have to set build parameter --base-href ./
and also use relative paths for the assets like images.
So the build command will actually be
ng build --prod --aot --service-worker --base-href ./
回答2:
Use http-server for running your built code locally
npm install http-server
http-server --help
http-server -p 8080 -c-1 dist/<project-name>
-p
specifies port, -c-1
disables caching and the last parameter is what directory should be served.
If you are playing with service workers and want to test them, this is good source of info.
回答3:
after compile your project for production
--prod
the output files after compiling you can't just open it from you browser you have to use local server
on your machine to do so you can use XAMPP
回答4:
As said you can use ng build --prod --base-href ./
.
Another option is to configure your angular.json file.
You have to add the following line under angular.json > projects > yourProject > architect > build > configurations > production
:
"baseHref": "./"
来源:https://stackoverflow.com/questions/53234127/angular-7-production-build-does-not-load-styles-and-js