在Angular 7中,如果在[style.background-image]
属性中设置动态图像路径但无法显示,可能是由于路径解析问题导致的。以下是解决方法的代码示例:
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imagePath = 'assets/images/image.jpg';
}
angular.json
文件中正确配置了图像文件的路径。"assets": [
"src/favicon.ico",
"src/assets"
]
确保assets
数组中包含了图像文件所在的文件夹。
src/assets
文件夹下的子文件夹中,确保在路径中正确指定子文件夹。
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imagePath = 'assets/images/subfolder/image.jpg';
}
请根据实际情况调整代码中的路径。这些解决方法可以帮助您在Angular 7中正确显示动态图像路径。
上一篇:Angular 7中的“Angular Auth拦截器无法刷新令牌”
下一篇:Angular 7中的“Dynamic Carosel images not showing with ngFor”的问题是动态轮播图的图片在使用ngFor时没有显示。