1

嗨,我正在尝试构建一个可重用的函数,并清理一些内容,但我不知道怎么称呼DomSanitizer它总是给我错误,即抽象类。这是我的功能:

export function PostFormat(post){
  let sanitizer: DomSanitizer; // TODO!

  post['title'] = sanitizer.bypassSecurityTrustHtml(post['title']['rendered']);
  post['author'] = post['_embedded']['author'][0];
  post['content'] = sanitizer.bypassSecurityTrustHtml(post['content']['rendered']);
  post['excerpt'] = sanitizer.bypassSecurityTrustHtml(post['excerpt']['rendered']);

  if (post['_embedded']['wp:featuredmedia']){
    if (post['_embedded']['wp:featuredmedia'][0]['media_details']){
      post['featured_image'] = post['_embedded']['wp:featuredmedia'][0]['media_details']['sizes'][this.default_size]['source_url'];
    }
  }
  if (post['_embedded']['replies']){
    post['comments'] = post['_embedded']['replies'][0];
  }
  return post;
}
4

1 回答 1

1

使用它的简单方法是添加导入

import { DomSanitizer } from '@angular/platform-browser';

之后,您可以使用消毒剂的所有功能

func trueHTML(post) {
  return this.sanitizer.bypassSecurityTrustHtml(
    post.replace(
      regex,
      match => `<a target="_blank" href="${match}">${match}</a>`
    )
  );
}

并添加您的文字inner html

<div [innerHtml]="post"></div>
于 2018-01-06T18:22:31.683 回答