我想创建一个处理所有 YouTube 嵌入视频的组件。但是,将 src 作为变量传递根本不起作用。不管我做什么。任何人都知道问题是什么,还是需要修复的角度错误?
我在父模板中有这个
<ng-container *ngFor="let link of youtubeUrlList">
<app-youtube-card [url]="link"></app-youtube-card>
</ng-container>
子组件模板是:
<iframe [src]="safeSource"></iframe>
在子组件类中:
export class YoutubeCardComponent implements OnInit {
@Input() url?: string
safeSource?: SafeResourceUrl
constructor(private sanitizer: DomSanitizer) { }
ngOnInit(){
this.safeSource = this.sanitizer.bypassSecurityTrustResourceUrl(this.url)
}
}
并得到这个错误
core.js:6456 ERROR TypeError: t.replace is not a function
at HTMLIFrameElement.set (<anonymous>:5:5482)
at EmulatedEncapsulationDomRenderer2.setProperty (platform-browser.js:739)
at elementPropertyInternal (core.js:10006)
at Module.ɵɵproperty (core.js:14764)
at YoutubeCardComponent_ng_container_1_Template (youtube-card.component.html:3)
at executeTemplate (core.js:9579)
at refreshView (core.js:9445)
at refreshEmbeddedViews (core.js:10570)
at refreshView (core.js:9469)
at refreshComponent (core.js:10616)
但是,如果我将任何 YouTube 链接直接放在 iframe 源属性中,我不会收到该错误