reactive方法虽然没报错但是没有响应

来源:3-12 vue3 模块化妙用- 鼠标追踪器

淡语

2021-05-26

App.vue 文件代码如下:

<template>
  <img alt="Vue logo"
       src="./assets/logo.png">
  <h1> {{count}} </h1>
  <h1> {{double}} </h1>
  <h1> {{greetings}} </h1>
  <h1> X: {{x}}, Y: {{y}} </h1>
  <button @click="increase"> 👍+1 </button>
  <button @click="updateGreeting"> Update Title </button>
</template>

<script lang="ts">
import {
  defineComponent,
  ref,
  computed,
  reactive,
  toRefs,
  watch
  // onMounted,
  // onUnmounted
} from 'vue'
import useMousePosition1 from './hooks/useMousePosition1'

interface DataProps {
  count: number
  double: number
  increase: () => void
}

export default defineComponent({
  name: 'App',
  setup() {
    const data: DataProps = reactive({
      count: 0,
      increase: () => {
        data.count++
      },
      double: computed(() => data.count * 2)
    })
    const { x, y } = useMousePosition1()
    const greetings = ref('')
    const updateGreeting = () => {
      greetings.value += 'Hello!'
    }

    watch([greetings, () => data.count], (newValue, oldValue) => {
      console.log(newValue)
      console.log(oldValue)
      document.title = 'updated' + greetings.value
    })
    const refData = toRefs(data)
    return {
      ...refData, // 展开后为 count, increase, double
      greetings,
      updateGreeting,
      x,
      y
    }
  }
})
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

src/hooks 目录下新建 useMousePosition1.ts 文件的代码如下:

// import { reactive, toRefs, onMounted, onUnmounted } from "@vue/reactivity";
import { reactive, toRefs, onMounted, onUnmounted } from 'vue'

function useMousePosition1() {
	const xyPosition = reactive({
		x: 0,
		y: 0,
		updateMouse: (e: MouseEvent) => {
			xyPosition.x = e.pageX
			xyPosition.y = e.pageY
		},
		onMounted: () => {
			document.addEventListener('click', xyPosition.updateMouse)
		},
		onUnmounted: () => {
			document.removeEventListener('click', xyPosition.updateMouse)
		}
	})
	const refxyPosition = toRefs(xyPosition)
	refxyPosition.x
	refxyPosition.y

	// return { refxyPosition.x,  refxyPosition.y }
	return { x: refxyPosition.x,  y: refxyPosition.y }
}

export default useMousePosition1

想必是钩子函数在reactive中的用法错了,请老师指教。

写回答

2回答

淡语

提问者

2021-05-27

已解决,谢谢老师的指导!

useMousePosition1.ts

//img.mukewang.com/szimg/60af05fe09af7c2a19201040.jpg

App.vue中调用就行啦!


0
0

张轩

2021-05-27

同学你好 

	// 这里写错了欧,里面的 updateMouse, onMounted,  onUnmounted 不能写在
	reactive 里面,都应该挪到外面来,reactive 只是一个响应式对象,和这些生命周期的
	钩子函数没关系
	const xyPosition = reactive({
		x: 0,
		y: 0,
		updateMouse: (e: MouseEvent) => {
			xyPosition.x = e.pageX
			xyPosition.y = e.pageY		},
		onMounted: () => {
			document.addEventListener('click', xyPosition.updateMouse)
		},
		onUnmounted: () => {
			document.removeEventListener('click', xyPosition.updateMouse)
		}
	})


0
1
淡语
谢谢老师!o(* ̄▽ ̄*)ブ
2021-05-27
共1条回复

Vue3 + TS 仿知乎专栏企业级项目

带你完成前后端分离复杂项目,率先掌握 vue3 造轮子技能

3142 学习 · 2313 问题

查看课程