【问题标题】:How to embed vimeo video in angular component?如何在角度分量中嵌入 vimeo 视频?
【发布时间】:2020-08-12 03:36:06
【问题描述】:

我有一个 Angular 8 应用程序。我尝试嵌入 vimeo 视频。

所以我有这样的ts:

getVideoId(url, prefixes) {
    const cleaned = url.replace(/^(https?:)?\/\/(www\.)?/, '');
    for (let i = 0; i < prefixes.length; i++) {
      if (cleaned.indexOf(prefixes[i]) === 0) {
        return cleaned.substr(prefixes[i].length);
      }
    }
    return undefined;
  }

  getVimeoId(url) {
    return this.getVideoId(url, ['vimeo.com/', 'player.vimeo.com/']);
  }

 getVideoSafeUrl(url: string): SafeResourceUrl {

    const embedUrl = this.parseYoutubeUrl(url);
    const safeUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(
      this.parseVimeo(embedUrl));

   return safeUrl;
  }

和这样的模板:

 <iframe
            *ngIf="videoUrl.value"
            class="video-iframe"
            type="text/html"
            [src]="getVideoSafeUrl(videoUrl.value)"
            frameborder="0"
            allowfullscreen
          ></iframe>

但是当我尝试插入 vimeo 视频时:https://vimeo.com/346340496/11432ab1db

我收到此错误:

VM7131 vendor.js:76269 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '346340496'
Error: Cannot match any routes. URL Segment: '346340496'
    at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2432)
    at CatchSubscriber.selector (router.js:2413)
    at 

那么我要改变什么?

谢谢

【问题讨论】:

    标签: javascript angular typescript embed vimeo


    【解决方案1】:

    尽量不要:

    https://vimeo.com/346340496/11432ab1db
    

    但在iframe 中用作[src] 时,格式低于url

    https://player.vimeo.com/video/346340496
    

    您需要相应地编写解析器,例如

      function  parseVimeo(url) {
        const re = /\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i;
        const matches = re.exec(url);
        return matches && "https://player.vimeo.com/video/"+matches[1];
      }
    
    

    确保您已经针对所有网址进行了测试,而不仅仅是 https://vimeo.com/346340496/11432ab1db 。当还有一些未知的 url 时,放置适当的用户消息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-16
      • 2017-01-03
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2014-12-26
      相关资源
      最近更新 更多