Angular 7构建应用程序-用实际URL替换资产URL [英] angular 7 build app - replace assets url with real url
问题描述
在Angular 7应用程序中,我拥有不同的资产,图像,字体等.它们都位于src/assets/images或src/assets/fonts等中
In an Angular 7 app i have different assets, images, fonts etc. All are located in src/assets/images or src/assets/fonts, etc
我的文件按如下方式使用它们:< img src ="/assets/images/img.png">
I my files i use them like this: <img src="/assets/images/img.png">
angular.json看起来像这样:
angular.json look like this:
.....
"assets": [
"src/favicon.ico",
"src/assets"
]
.....
我在建造时使用:
ng build --prod --build-optimizer --deploy-url = https://cdn.domain.com
这将从此处替换所有对资产资源的引用:
This will replace all the reference to assets resource from this:
< img src ="/assets/images/img.png">
对此:
< img src ="https://cdn.domain.com/assets/images/img.png">
我的问题是如何在构建时删除 assets/images
路径,以便在构建后输出为:
My question how I can remove the assets/images
path when building so that after building the output will be:
< img src ="https://cdn.domain.com/img.png">
推荐答案
您可以替换
"assets": [
...
"src/assets",
...
]
通过
"assets": [
...
{ "glob": "**/*", "input": "src/assets", "output": "/" },
...
]
所有文件都将位于"/dist/environment/"而不是"/dist/environment/..."
All the files will be in "/dist/environment/" and not in "/dist/environment/..."
然后在您的html文件中,直接使用:
Then in your html files, use directly :
<img src="/img.png">
在这里看看:https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/asset-configuration.md
:-)
这篇关于Angular 7构建应用程序-用实际URL替换资产URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!