Angular-清理图像URL的努力

时间:2020-10-16 12:21:23

标签: angular sanitization

我尝试对图像URL进行清理,但是我将无法为我服务...

这是我的代码

home.component.ts

  constructor(public dialog: MatDialog, private route: ActivatedRoute, private sanitization : DomSanitizer) {
     }

     user: any;
     img: SafeStyle;



  ngOnInit(): void {
    this.route.data.subscribe((data: {user: any}) => { this.user = data.user;});
    console.log('Data: ', this.user);
    this.img = this.sanitization.bypassSecurityTrustStyle('url(${this.user.profileImagePath})');
  }

home.component.html

<div class="create_post_avatar" ngIF='img'>
    <img [src]='img'>
</div>

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试将消毒方法更改为bypassSecurityTrustResourceUrl

this.img = this.sanitization.bypassSecurityTrustResourceUrl('url(${this.user.profileImagePath})');