angular 7 build app - replace assets url with real url

試著忘記壹切 提交于 2021-01-29 05:21:12

问题


In an Angular 7 app i have different assets, images, fonts etc. All are located in src/assets/images or src/assets/fonts, etc

I my files i use them like this: <img src="/assets/images/img.png">

angular.json look like this:

 .....
 "assets": [
          "src/favicon.ico",
          "src/assets"
        ]
 .....

When building I use:

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">

to this:

<img src="https://cdn.domain.com/assets/images/img.png">

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">


回答1:


You could replace

 "assets": [
    ...
    "src/assets",
    ...
  ]

by

"assets": [
  ...
  { "glob": "**/*", "input": "src/assets", "output": "/" },
  ...
]

All the files will be in "/dist/environment/" and not in "/dist/environment/..."

Then in your html files, use directly :

<img src="/img.png">

Have a look here : https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/asset-configuration.md

:-)



来源:https://stackoverflow.com/questions/53962936/angular-7-build-app-replace-assets-url-with-real-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!