Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue 两个数据互相 watch 为什么不会无限循环 #152

Open
lovelmh13 opened this issue Jul 26, 2021 · 0 comments
Open

vue 两个数据互相 watch 为什么不会无限循环 #152

lovelmh13 opened this issue Jul 26, 2021 · 0 comments

Comments

@lovelmh13
Copy link
Owner

有如下代码:

// app.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" width="25%" />
<HelloWorld :msg.sync="msg" :lastPage="thispage" />
<input type="text" v-model="thispage" />
<input type="text" v-model="msg" />
</div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
name: "App",
data() {
return {
msg: "msg",
thispage: "1122",
};
},
components: {
HelloWorld,
},
};
</script>
// helloword.vue
<template>
<div class="hello">
<input type="text" v-model="msgCopy" />
</div>
</template>
 
<script>
export default {
name: "HelloWorld",
props: {
msg: {
type: String,
default: "",
},
lastPage: String,
},
watch: {
 lastPage(newVal, oldVal) {
 console.log("watch:lastPage", newVal, oldVal);
},
msg: {
 handler(newVal, oldVal) {
 debugger;
 console.log("watch:msg", newVal, oldVal);
 this.msgCopy = newVal;
},
},
msgCopy: {
 handler(newVal, oldVal) {
 console.log("watch:msgCopy", newVal, oldVal);
 this.$emit("update:msg", newVal);
},
},
},
 mounted() {
 this.msgCopy = this.msg;
},
 data() {
 return {
msgCopy: "",
};
},
};
</script>

打印效果如下:msg 和 msgCopy 互相修改,并且都对自身进行了 watch,但是修改了 msgCopy, 并不会一直陷入 watch 的无限循环中。

watch:msgCopy hello world1 hello world
 
// 中间是父组件更新 msg 的动作
 
watch:msg hello world1 hello world

原因

msg 是 基本类型,如果改成 复杂类型,则会死循环:

<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" width="25%" />
<HelloWorld :msg.sync="msg" :lastPage="thispage" />
<input type="text" v-model="thispage" />
<input type="text" v-model="msg.text" />
</div>
</template>
 
<script>
import HelloWorld from "./components/HelloWorld";
 
export default {
name: "App",
 data() {
 return {
msg: {
text: "msg",
},
thispage: "1122",
};
},
components: {
HelloWorld,
},
};
</script>
<template>
<div class="hello">
<input type="text" v-model="msgCopy" />
</div>
</template>
 
<script>
export default {
name: "HelloWorld",
props: {
msg: {
type: Object,
 default() {
 return {};
},
},
lastPage: String,
},
watch: {
 lastPage(newVal, oldVal) {
 console.log("watch:lastPage", newVal, oldVal);
},
msg: {
 handler(newVal, oldVal) {
 debugger;
 console.log("watch:msg", newVal, oldVal);
 this.msgCopy = newVal;
},
},
msgCopy: {
 handler(newVal, oldVal) {
 console.log("watch:msgCopy", newVal, oldVal);
 this.$emit("update:msg", { text: newVal });
},
},
},
 mounted() {
 this.msgCopy = this.msg;
},
 data() {
 return {
msgCopy: {
text: "copy",
},
};
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant