javascript - 如何在 Angular 分量中嵌入vimeo视频?

标签 javascript angular typescript embed vimeo

我有一个 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 。当存在一些未知网址时,也放置适当的用户消息

关于javascript - 如何在 Angular 分量中嵌入vimeo视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61478227/

相关文章:

Angular2 和 TweenMax - 无法在 'scrollTo' : parameter 1 'Window' ) is not an object 上执行 ('options'

angular - 如何在 agRichSelectCellEditor 中更改下拉列表的宽度

javascript - URLSearchParams 返回空对象

javascript - 使用 TypeScript 泛型定义 Factory 方法的返回类型

javascript - 如何直接打开 Amazon Cognito 托管 UI 的注册页面?

javascript - 从生成器迁移到异步/等待

javascript - 使用带有空输入 js 的 shift

Javascript上传功能

angular - 在请求 header 中设置 Cookie Angular2

javascript - 测试函数名称是否包含字符串,函数是否返回可观察对象