3

切换到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>');
  }
}

我的问题是如何转换SafeHtmlSafeUrl字符串?刚刚.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

在此处输入图像描述

4

1 回答 1

1

这似乎是一个错误,现在这是答案:https ://github.com/angular/angular/issues/33028

于 2019-10-07T17:54:47.823 回答