51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

js的Object.assign坑

志当存高远。------诸葛亮

今天遇到一个坑

|------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | hljs vue <template> <div> <el-form> <el-form-item> <el-input v-model="user.name"></el-input> <el-input v-model="user.age"></el-input> <el-input v-model="user.gender"></el-input> <el-button type="primary" @click="toYoung">点我</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { user: {name: "achao", age: 20}, info: {name: "ruben", age: 18, gender: "男"} } }, created() { console.log(this.$route) }, methods: { toYoung() { Object.assign(this.user, {name: "ruben", age: 18, gender: "男"}) console.log(this.user) } }, } </script> |

这里可以看到

我们点击按钮后调用toYoung

里面执行了Object.assign(this.user, {name: "ruben", age: 18, gender: "男"})

这个函数是用后面的参数填充前面的参数

我们执行前页面如下

image-20210419204318898

执行后

image-20210419204329549

表面看上去好像OK

实际上我们在第三个user.genderinput框中输入值时发现输入不了!

这个BUG解决办法也很简单

改成这种方式就可以啦

|-----------------|----------------------------------------------------------------------------------------| | 1 2 3 4 | hljs vue this.user = { ...this.user, ...{name: "ruben", age: 18, gender: "男"} } |

赞(0)
未经允许不得转载:工具盒子 » js的Object.assign坑