Angular可以在资源文件夹之外提供图像吗 [英] Can Angular serve images outside assets folder

查看:79
本文介绍了Angular可以在资源文件夹之外提供图像吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular上建立了一个项目,该项目将在设计中包含大量图像.我想知道是否有可能将每个组件使用的图像保留在组件内,而不必从资产文件夹中获取每个图像.

Am building a project on Angular and this project will have lots of images as part the design. Am wondering if it is possible to keep images used by each component within the component instead of having to fetch each image from the assets folder.

我想知道是否可以配置Angular来容纳和提供该组件中某个组件使用的图像.

I want to know if it is possible to configure Angular to house and serve images used by a component within that component.

查看图片以进行澄清:

推荐答案

资产包含在构建应用程序时按原样复制的图像文件和其他资产文件.

Assets Contains image files and other asset files to be copied as-is when you build your application.

但是,可以!

但是为此,您需要手动要求组件文件中的每个图像,并在这样的模板中动态使用-

But for this you need to manually require each image in your component file and use dyanmically in template like this -

declare var require: any
myImage = require('./myImage.png');

<img [src]="myImage" />

默认情况下,Angular CLI对assets文件夹中的静态数据(如图像/字体/Css文件等)进行了配置.

As by default Angular CLI has a configuration for static data such as Images/Fonts/Css files etc from assets folder.

但是,如果您想从那里之外使用一些静态数据,则需要像上面提到的那样.

But if you want to use some static data outside from there you need to require like above mentioned.

PS :不确定在构建时angular是否要获取所有静态数据,例如组件级别的图像.但应该可以在ng serve

PS: Not sure whether angular takes all static data at the time of build such as image at the component level. But should work in ng serve

这篇关于Angular可以在资源文件夹之外提供图像吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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