【vue3】四、onMounted、onCreated等使用
创建文件useMouse.js从vue模块引入onMounted,ref在onMounted中定义方法import {onMounted, ref} from 'vue'export default function useMouse() {let x = ref(0)let y = ref(0)onMounted(() => {window.addEventListener('mousem
·
- 创建文件useMouse.js
- 从vue模块引入onMounted,ref
- 在onMounted中定义方法
import {onMounted, ref} from 'vue'
export default function useMouse() {
let x = ref(0)
let y = ref(0)
onMounted(() => {
window.addEventListener('mousemove', e => {
x.value = e.pageX
y.value = e.pageY
})
})
return {x, y};//x,y可以在组件中使用
}
更多推荐
已为社区贡献1条内容
所有评论(0)