关于前端:记录vue30的坑watch新旧数据相同

1次阅读

共计 591 个字符,预计需要花费 2 分钟才能阅读完成。

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,这样能够监听到批改前和批改后的值

正文完
 0