Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 切换到查看youtube视频无效_Angular_Video_Iframe_Youtube_Switch Statement - Fatal编程技术网

Angular 切换到查看youtube视频无效

Angular 切换到查看youtube视频无效,angular,video,iframe,youtube,switch-statement,Angular,Video,Iframe,Youtube,Switch Statement,我创建了一个组件,它应该根据输入中传递给它的属性来显示视频。。。如果我插入静态数据,它对我有效,但是使用开关否 export class VideoComponent implements OnInit { @Input() currentVideo: string | undefined; url: any; video: any = { id: 'yOYmdyaSOCg' }; baseUrl: string = 'https://www.youtube.com/e

我创建了一个组件,它应该根据输入中传递给它的属性来显示视频。。。如果我插入静态数据,它对我有效,但是使用开关

 export class VideoComponent implements OnInit {
   @Input() currentVideo: string | undefined;
   url: any;
   video: any = { id: 'yOYmdyaSOCg' };
   baseUrl: string = 'https://www.youtube.com/embed/';

   constructor(
     private sanitizer: DomSanitizer,
   ) {
     switch (this.currentVideo) {
      case 'foo':
        this.video.id = 'yOYSOCsdsdg';
         break;
       case 'bar':
        this.video.id = '3Z4J_bddKE';
         break;
       case 'foo2':
         this.video.id = 'a8DPNc64';
         break;
       case "bar2":
         this.video.id = 'HZySqMdsclEuSQ';
         break;
       default:
         this.video.id = 'TYYW_WsdwYHuM';
     }
     this.url = this.sanitizer.bypassSecurityTrustResourceUrl(
       this.baseUrl + this.video.id
     );

   }

 }

从代码的外观来看,似乎您正在实现OnInit接口,但尚未添加ngOnInit方法。最好添加ngOnInit方法,然后根据其中的输入绑定而不是构造函数中的输入绑定做出决策。这是因为只有在解决了所有输入绑定之后,才会调用OnInit生命周期挂钩。请查看此处以了解区别:。静态数据工作的原因是,它就像用一些已经可用的字符串值设置字段一样,而输入绑定显然不是这样,因为它们尚未在构造函数中解析


所以,您需要做的只是将构造函数中的代码移动到ngOnInit方法,并让消毒剂保持在构造函数参数中的状态。依赖项注入器将确保DOMSanitizer被注入。应该就是这样。

非常感谢!另一件事,你认为有没有更干净的方法来避免切换?对我来说,这似乎不是一个最好的做法,但我不确定,我是新手。