切换到 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()
好吗?
角度SafeHtml
声明为:
/**
* Marker interface for a value that's safe to use as HTML.
*
* @publicApi
*/
export declare interface SafeHtml extends SafeValue {
}
并且在lib.dom.d.ts中定义了[innerHtml]:
interface InnerHTML {
innerHTML: string;
}
innerHtml
的类型为string
,而我有SafeHtml
。
如何复制:
git clone https://github.com/felikf/angular-repro-safe-html.git
npm i
npm run build