surmon-china / videojs-player

@videojs player component for @vuejs(3) and React.
https://github.surmon.me/videojs-player
MIT License
5.23k stars 1.12k forks source link

无法调用暂停视频的方法 #441

Closed lixinliA closed 2 years ago

lixinliA commented 2 years ago

Describe the bug

我在vue3+ts中使用无法调用暂停视频的方法

Reproduction

image image image

System Info

谷歌浏览器

Used Package Manager

pnpm

Validations

surmon-china commented 2 years ago

可能的排查方向:timeUpdate 事件可能会在 mounted 之前执行,如果是,则此时 videoRef 确实为空,加个非空判断或者 ?. 运算符即可。

surmon-china commented 2 years ago

另外:PlayerState 是一个 readonly 的响应式对象,天然基于 Vue 系统且自洽,所以如果你需要消费(衍生)一些播放器状态,应尽量使用 computed,如:

const remainingTime = computed(() => {
  return state.value
    ? state.value.duration - state.value.currentTime
    : 0
})

副作用可以基于数据状态的变化,而非事件,如:

watch(
  () => state.value?.currentTime,
  (time) => {
    if (time === 5) {
      // do something ...
    }
  }
)