1. 创建文件useMouse.js
  2. 从vue模块引入onMounted,ref
  3. 在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可以在组件中使用
}
Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐