属性“玩家”在类型上不存在

时间:2019-06-22 14:32:27

标签: angular typescript

这是我的代码:

export class Test implements OnInit {

  constructor() {
  }

  ngOnInit() {
    const YTPlayer = require('yt-player');
    const player = new YTPlayer('#player');

    player.load('GKSRyLdjsPA');
    player.setVolume(100);

    player.on('playing', () => {
      console.log(player.getDuration())
    });
  }

  jumpToPoint(time){
    this.player.seek(time);
    this.player.play();
  }

}

他是我的错误:

  

属性“玩家”在类型上不存在

我不知道如何在NgOnInit()中声明玩家,并在以下代码中使用玩家变量。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

将变量声明为const将使它的作用域成为周围的作用域,在本例中为ngOnInit函数。
您应该将其声明为类属性:

const YTPlayer = require('yt-player');
// import YTPlayer from "yt-player"; - This is the same

export class Test implements OnInit {
  private player: YTPlayer; 

  ngOnInit() {
    this.player = new YTPlayer('#player');
    // Rest of initialization...
  }

  jumpToPoint(time){
    this.player.seek(time); // Should work!
    this.player.play();
  }
}

For further reference

答案 1 :(得分:0)

使用此:

import * as YTPlayer from 'yt-player'
export class Test implements OnInit {

  constructor() {
  }
private player:YTPlayer
  ngOnInit() {
 this.player = new YTPlayer('#player');

    player.load('GKSRyLdjsPA');
    player.setVolume(100);

    player.on('playing', () => {
      console.log(player.getDuration())
    });
  }

  jumpToPoint(time){
    this.player.seek(time);
    this.player.play();
  }

}