首页 > 解决方案 > 如何在角度 6 中设置背景图像?

问题描述

在这里,我想设置占位符图像,为此我使用 commonurl 路径(this.commonUrlObj.commonUrl)而不是(http://localhost:3000),那么如何使用 angular 6 中的 commonurl 在背景图像中设置图像?

category.component.html

<img [src]="url" style="height: 400px;width: 400px" [ngStyle]="{'background-image': getUrl()}">

类别.component.ts

getUrl(){
  return "url('this.commonUrlObj.commonUrl/placeholder.jpg')";
}

普通类.ts

export class CommonClass {
  constructor(public commonUrl : string = 'http://localhost:3000'){};
}

标签: htmlcssangularangular6

解决方案


getUrl(){
  return "url('"+this.commonUrlObj.commonUrl+"'/placeholder.jpg')";
}

推荐阅读