我需要创建一个 Angular 2+(我在 4.x)指令(不是组件),通过@HostBinding
. 我知道我很接近了,但是当我检查它时,我会background-image: none
在 Chrome 的检查器中看到它。
import { Directive, HostBinding } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';
@Directive({
selector: '[myDirectiveWithBackgroundImage]'
})
export class MyDirective {
@HostBinding('style.background-image')
public backgroundImage: SafeStyle;
constructor(private sanitizer: DomSanitizer) {
this.backgroundImage = this.sanitizer.bypassSecurityTrustStyle(
'url(assets/images/favicon16.png) no-repeat scroll 7px 7px;'
);
}
}
我的 assets/images/favicon.16png 由 webpack 提供服务。我已经尝试了各种路径选项,,,/assets
等等~/assets
......但background-image
总是none