1.先上代码
<script>import { reactive ,toRefs, ref ,watch} from "vue";import { useRouter } from "vue-router";export default { setup() { const state = reactive({ password: null, username: null, }); // const router = useRouter(); // const goto_home = (() => { // router.replace( '/userList') // }) const affirm = ()=>{ console.log('enter') } watch(()=>state.username, (newValue, oldValue) => { //间接监听 console.log("扭转了",newValue,oldValue,state);}); return { affirm, ...toRefs(state) }; },};</script>
这是正确的监听形式
2.如果是监听全副时
watch(state, (newValue, oldValue) => { //间接监听 console.log("count扭转了",newValue,oldValue,state);});
newValue和 oldValue是一样的(都是批改后的值)
举荐形式1,这样能够监听到批改前和批改后的值