Angular Ivy 类型检查:类型 'SafeHtml' 不可分配给类型 'string'
Angular Ivy type check: Type 'SafeHtml' is not assignable to type 'string'
我在切换到 Ivy 编译器后出现 Typescript 错误:
[Step 4/5] src/app/app.component.html(1,26): Type 'SafeHtml' is not assignable to type 'string'.
在 Angular class 中有一个成员 属性 声明为 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()
可以吗?
Angulars SafeHtml
声明为:
/**
* 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
这似乎是一个错误,现在这是答案:https://github.com/angular/angular/issues/33028
我在切换到 Ivy 编译器后出现 Typescript 错误:
[Step 4/5] src/app/app.component.html(1,26): Type 'SafeHtml' is not assignable to type 'string'.
在 Angular class 中有一个成员 属性 声明为 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()
可以吗?
Angulars SafeHtml
声明为:
/**
* 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
这似乎是一个错误,现在这是答案:https://github.com/angular/angular/issues/33028