Showing
2 changed files
with
20 additions
and
6 deletions
| ... | @@ -17,6 +17,8 @@ import { onMounted } from 'vue'; | ... | @@ -17,6 +17,8 @@ import { onMounted } from 'vue'; |
| 17 | import banner from './banner'; | 17 | import banner from './banner'; |
| 18 | import videoBar from './videoBar'; | 18 | import videoBar from './videoBar'; |
| 19 | import status from './status'; | 19 | import status from './status'; |
| 20 | +import { useEventListener } from '@/composables'; | ||
| 21 | + | ||
| 20 | // 视频基础属性 | 22 | // 视频基础属性 |
| 21 | const props = defineProps({ | 23 | const props = defineProps({ |
| 22 | item: Object, | 24 | item: Object, |
| ... | @@ -41,13 +43,12 @@ onMounted(() => { | ... | @@ -41,13 +43,12 @@ onMounted(() => { |
| 41 | }); | 43 | }); |
| 42 | const video = mp.video(); | 44 | const video = mp.video(); |
| 43 | // 监听原生video事件 | 45 | // 监听原生video事件 |
| 44 | - video && | 46 | + useEventListener(video, 'play', (event) => { |
| 45 | - video.addEventListener('play', function (event) { | 47 | + emit('on-play', { |
| 46 | - emit('on-play', { | 48 | + event, |
| 47 | - event, | 49 | + props: props.item, |
| 48 | - props: props.item, | ||
| 49 | - }); | ||
| 50 | }); | 50 | }); |
| 51 | + }); | ||
| 51 | }); | 52 | }); |
| 52 | </script> | 53 | </script> |
| 53 | 54 | ... | ... |
| 1 | +import { onMounted, onUnmounted } from 'vue' | ||
| 2 | + | ||
| 1 | import { useVideoList } from '@/composables/useVideoList.js' | 3 | import { useVideoList } from '@/composables/useVideoList.js' |
| 2 | import { useDefaultPerf } from '@/composables/useDefaultPerf.js' | 4 | import { useDefaultPerf } from '@/composables/useDefaultPerf.js' |
| 3 | import { useBookList } from '@/composables/useBookList.js' | 5 | import { useBookList } from '@/composables/useBookList.js' |
| ... | @@ -7,3 +9,14 @@ export { | ... | @@ -7,3 +9,14 @@ export { |
| 7 | useDefaultPerf, | 9 | useDefaultPerf, |
| 8 | useBookList | 10 | useBookList |
| 9 | } | 11 | } |
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * 添加和清除 DOM 事件监听器 | ||
| 15 | + * @param {*} target | ||
| 16 | + * @param {*} event | ||
| 17 | + * @param {*} callback | ||
| 18 | + */ | ||
| 19 | +export function useEventListener(target, event, callback) { | ||
| 20 | + onMounted(() => target && target.addEventListener(event, callback)) | ||
| 21 | + onUnmounted(() => target && target.removeEventListener(event, callback)) | ||
| 22 | +} | ... | ... |
-
Please register or login to post a comment