我目前正在研究 Angular 2,我对如何将样式和脚本注入(inject) index.html
有疑问通过 Angular CLI build
命令(我指的是在 angular-cli.json
中定义的脚本和样式)。
问题是我在嵌套位置运行应用程序,例如:
http://domain.com/my_app
同时 <base href="/my_app">
可以设置,注入(inject)的脚本和样式总是指向src
到根域。即 /
而不是 /my_app
.当然,这会在尝试查看页面时导致 404。
我正在服务 /dist
直接。
Nginx 配置:
server {
server_name domain.com;
listen 80;
location /my_app {
alias /srv/www/my_app;
index index.html;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
gzip on;
gzip_types text/css text/javascript application/x-javascript application/json;
}
在哪里/srv/www/my_app
主要由 /dist
组成运行后包含:
ng build --environment=prod --base-href /my_app
我可以手动为输出中的样式和脚本声明添加前缀 index.html
但在我看来,这只是一项不必要的忙碌工作。
有没有办法为 ng build
中的注入(inject)样式和脚本声明指定基本 href?生成 index.html
?如果我可以根据目标环境指定基本 href,那就更好了,例如:
-
/my_app
用于生产 -
/
发展
最佳答案
您是否在执行 ng build
命令时尝试使用 --deploy-url
参数?此选项表示 URL where files will be deployed
因此在您的情况下,您可以尝试运行以下命令:
ng build --environment=prod --base-href my_app --deploy-url my_app
关于javascript - 如何在 Angular 2 中为脚本和样式指定基本 href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40936260/