问题描述
我遇到了必须为Angular中的iframe清理URL的问题,但是How to set <iframe src="..."> without causing `unsafe value` exception?中提供的公认答案似乎对我不起作用。
引发的错误为 ERROR错误:资源URL上下文中使用的不安全值
我找到的解决方案在下面的代码中显示,在注释here
中可见import { Pipe,PipeTransform,SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'safe'
})
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) { }
transform(url) {
// commented return throws error,both with URL and with RESOURCE_URL. idk why
// return this.sanitizer.sanitize(SecurityContext.URL,url);
return this.sanitizer.bypassSecurityTrustResourceUrl(this.sanitizer.sanitize(SecurityContext.URL,url))
}
}
该管道在共享模块中声明并导出,并在iframe广告代码中使用(目前,我使用YouTube视频中的占位符网址)
import { NgModule } from '@angular/core';
import { SafePipe } from './pipes/safe.pipe';
@NgModule({
declarations: [SafePipe],exports: [SafePipe],})
export class SharedModule { };
html:
<iframe width="680" height="430" [src]="courseIframeUrl | safe" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
我的问题是,这正确吗?对于第一个链接的公认答案表明,bypassSecurityTrustResourceUrl是不安全的,但是the documentation要求将方法用于iframe或脚本。如果没有,正确的解决方案会是什么样子?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)