3

属性'nativeElement',我在尝试将vimeo/player.js集成到我的angular-cli应用程序中时遇到了一些问题。vimeo/player.js - 无法读取标题中所述的未定义

由于我还没有发现对角4任何支持库,我以下在以下步骤中的README.md与模块捆绑使用。

我建VIMEO-player.component.ts

import { Component, AfterViewInit, ViewChild} from '@angular/core'; 
import { Player } from '@vimeo/player'; 

@Component({ 
    selector: 'app-vimeo-video-player', 
    templateUrl: './vimeo-player.component.html' 
}) 

export class VimeoVideoComponent implements AfterViewInit { 
    @ViewChild('vimeoVideoContainer') vimeoVideoContainer; 
    public player: Player; 

    ngAfterViewInit() { 
    this.player = new Player(this.vimeoVideoContainer.nativeElement, { 
     id: 19231868, // Generic Id 
     width: 640 
    }); 
    } 

} 

VIMEO-player.component.html

<div id="vimeoVideoContainer"></div> 

而且我想里面使用它另一个模板的组件。比如里面my.component.ts的模板,我有:

<app-vimeo-video-player></app-vimeo-video-player> 

我越来越Cannot read property 'nativeElement' of undefined甚至以为我设法利用ngAfterViewInit。如果在触发new Player()...之前设置了超时时间,则会发生同样的情况。

我在vimeo/player.js issues上发现了这个问题,但它没有帮助我。

编辑1

随着@peinearydevelopment我解决了这个特别的错误,但现在我越来越:ERROR TypeError: __WEBPACK_IMPORTED_MODULE_1__vimeo_player__.Player is not a constructor

我在做什么错?我怎样才能解决这个问题?

回答

2

更改您的div:<div #vimeoVideoContainer></div>

虽然我不能找到很好的文档,这里是ViewChild documentation。他们的文档中有许多不同的地方,他们使用不同的选择器类型。正如你从文档中看到的那样,它会接受一个类似的,因为你没有选择一个自定义组件,所以在这里看起来并不适用。上面的语法是我在使用字符串选择元素时最常见的。

关于你的第二个编辑,尝试导入Player如下import * as Player from '@vimeo/player/dist/player.js';

+0

你能解释一下为什么吗?无论如何,我得到另一个错误'StartNetworkComponent.html:2错误类型错误:__WEBPACK_IMPORTED_MODULE_1__vimeo_player __。播放器不是一个构造函数。 – AndreaM16

+0

是的,我试图找到帮助解释的文档。 – peinearydevelopment