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

问题描述 投票:0回答:1

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

我得到这个错误。

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个回答
1
投票

尝试不。

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

但在下面 url 格式时,作为 [src]iframe

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];
  }

确保你已经测试了所有的URL,而不仅仅是 https://vimeo.com/346340496/11432ab1db . 当有一些未知的urls时,也要加上适当的用户信息。

© www.soinside.com 2019 - 2024. All rights reserved.