gpt4 book ai didi

angular - 警告 : sanitizing unsafe style value

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:26 24 4
gpt4 key购买 nike

对于 Angular 的第 7 版,我有这个简单的代码:

@Component({
selector: 'nf-delete',
template: `
<span [title]="title" class="mdi mdi-close-circle font-size-medium">
</span>
`,
styles: [
`
:host {
cursor: pointer;
line-height: 1;
vertical-align: middle;
}

:host(.inline) {
display: inline-block;
}
`
]
})
export class DeleteComponent {
@Input() title = 'Delete';
@Input() inline = false;

@HostBinding('class.inline')
get isInline() {
return this.inline;
}
}

我写了另一个版本:

@Component({
selector: 'nf-delete',
template: `
<span [title]="title" class="mdi mdi-close-circle font-size-medium">
</span>
`,
styles: [
`
:host {
cursor: pointer;
line-height: 1;
vertical-align: middle;
}
`
]
})
export class DeleteComponent {
@Input() title = 'Delete';

constructor(private readonly sanitizer: DomSanitizer, private el: ElementRef, private rendrer: Renderer2) {
}

@Input() set inline(inline: boolean) {
inline && this.rendrer.setStyle(this.el.nativeElement, 'display', this.sanitizer.bypassSecurityTrustStyle('inline-block'));
}
}

两个版本输出警告如下:

core.js:13290 WARNING: sanitizing unsafe style value [object Object] (see http://g.co/ng/security#xss).

有人知道如何解决这个警告吗?

最佳答案

使用HostBinding在更改检测期间在宿主元素上设置属性/样式。

我在这里为您的案例实现了一个小演示:https://stackblitz.com/edit/angular-k9sr9d

inline: boolean;
@HostBinding('style.display') get disp() {
return this.inline ? 'inline-block' : '';
}

关于angular - 警告 : sanitizing unsafe style value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53934081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com