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

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理