angular 7 构建应用程序 - 用真实 url 替换资产 url [英] angular 7 build app - replace assets url with real url

查看:22
本文介绍了angular 7 构建应用程序 - 用真实 url 替换资产 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:

为此:

我的问题是如何在构建时删除 assets/images 路径,以便构建输出后:

My question how I can remove the assets/images path when building so that after building the output will be:

推荐答案

你可以替换

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆