我收到以下错误:
Angular 2 - EXCEPTION:错误:资源URL上下文中使用的不安全值
是否与启动时不立即使用媒体项目有关?或者它与URL不安全有关?我正在努力消毒它。
export class HomeComponent {
sanitizer: DomSanitizationService;
errorMessage: string;
activeMedia: MediaItem = new MediaItem(0, '', '', '', '', '', '');
constructor(
private mediaStorage: MediaStorageService,
private harvesterService: HarvesterService,
sanitizer: DomSanitizationService) {
this.sanitizer = sanitizer;
// Initial call -
harvesterService.getMediaItems(10, 'type', 'category');
let subscription = harvesterService.initialMediaHarvestedEvent.subscribe(() => {
this.activeMedia = mediaStorage.mediaList[0];
let newURL = this.activeMedia.URL + '?rel=0&autoplay=1';
newURL = newURL.replace('watch?v=', 'v/');
this.activeMedia.URL = newURL; //sanitizer.bypassSecurityTrustUrl(newURL);
console.log(newURL);
});
}
cleanURL(oldURL: string): SafeResourceUrl {
return this.sanitizer.bypassSecurityTrustUrl(oldURL);
}
}
模板代码为:
<div class="row" >
<iframe
id="video"
class="video"
src="{{ cleanURL(activeMedia.URL) }}"
frameborder="0"
allowfullscreen>
</iframe>
</div>
答案 0 :(得分:66)
更新:更改
后 src="{{cleanURL(activeMedia.URL)}}"
为:
[src]="cleanURL(activeMedia.URL)"
我得到:原始异常:错误:需要安全的ResourceURL,有一个网址
通过将cleanURL方法中的代码更改为:
来解决 return this.sanitizer.bypassSecurityTrustResourceUrl(oldURL);
而不是:
return this.sanitizer.bypassSecurityTrustUrl(oldURL);
答案 1 :(得分:0)
试试这个:
cleanURL(oldURL: string): string {
return this._sanitationService.sanitize(SecurityContext.URL, oldURL);
}