[英]How to refer images in Angular HTML page
I'm using angular 5 and web pack (Not angular CLI). 我正在使用Angle 5和Web Pack(不是angular CLI)。 This is my folder structure. 这是我的文件夹结构。
-src
| -app
| - subfolder
| - subfolder
| - componenet.html
| -assets
| - test.png
When referring to this image in my HTML file I have to use a lot of backslashes. 在我的HTML文件中引用此图像时,我必须使用很多反斜杠。 The following code segment shows an example. 以下代码段显示了一个示例。
<div>
<h1>Menu Page</h1>
<img src="../../../../../assets/test.png">
</div>
Is their a more practile way to do this in angular ? 他们是更实用的方式来做到这一点吗? Im using file-loader
to bundle the images to the dist folder and html-loader
for html pages. 我使用file-loader
将图像捆绑到dist文件夹和html页面的html-loader
中。
In your project's angular.json file make sure that your project -> architect -> build -> options -> assets config is setup properly: 在项目的angular.json文件中,确保正确设置了项目->架构师->构建->选项->资产配置:
{
"build": {
"architect": {
"options": {
...
"assets": [
"src/favicon.ico",
"src/assets"
],
Then in your template you should be able to do: 然后,您应该可以在模板中执行以下操作:
<div>
<h1>Menu Page</h1>
<img src="/assets/test.png">
EDIT (if not using angular-cli, which is strongly suggested): 编辑(如果不使用angular-cli,强烈建议使用):
in your webpack.config.js 在您的webpack.config.js中
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
...
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
],
options: {
outputPath: 'assets',
},
}
]
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.