如何在 angular 组件中嵌入 vimeo 视频?

How to embed vimeo video in angular component?

我有一个 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 

那么我必须改变什么?

谢谢

尽量不要:

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 时也放置适当的用户消息