切换到Ivy 编译器后出现Typescript错误:
[Step 4/5] src/app/app.component.html(1,26): Type 'SafeHtml' is not assignable to type 'string'.
在 Angular 类中有一个成员属性声明为SafeHtml
:
@Component({
selector: 'app',
template: `<div [innerHTML]="description"></div>`
})
export class AppComponent {
description: SafeHtml;
constructor(private sanitizer: DomSanitizer) {}
ngOnInit(): void {
this.description = this.sanitizer.sanitize(SecurityContext.HTML, '<strong>whatever comes from server</strong>');
}
}
我的问题是如何转换SafeHtml
和SafeUrl
字符串?刚刚.toString()
好吗?
AngularsSafeHtml
被声明为:
/**
* Marker interface for a value that's safe to use as HTML.
*
* @publicApi
*/
export declare interface SafeHtml extends SafeValue {
}
而 [innerHtml] 在 lib.dom.d.ts 中定义:
interface InnerHTML {
innerHTML: string;
}
innerHtml
是类型的,string
而我有SafeHtml
。
如何重现:
git clone https://github.com/felikf/angular-repro-safe-html.git
npm i
npm run build