gpt4 book ai didi

javascript - 重置 v-model 而不更新其因变量

转载 作者:行者123 更新时间:2023-11-30 20:37:07 26 4
gpt4 key购买 nike

我有 <input>字段如

<input type="text" v-model=user.name" />
<input type="text" v-model="user.phone" />
<button @click="add">add user</button>

每当我点击 add user , 它应该推送 userusers数组并清除 v-model (为了推送更多的用户)。我的add()方法如下所示。

add()
{
this.users.push( this.user );
this.user.name = '';
this.user.phone = '';
}

但是在重置 user 之后v-model , users 中的元素数组也变成空字符串。如何重置 v-model不更改 users 中的数据数组?

最佳答案

最简单的方法就是重置整个 user 对象而不是逐个属性:

add()
{
this.users.push( this.user );
this.user = {name: '', phone: ''};
}

演示:

new Vue({
el: '#app',
data: {
user: {name: '', phone: ''},
users: []
},
methods: {
add() {
this.users.push(this.user);
this.user = {name: '', phone: ''};
}
}
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
name: <input type="text" v-model="user.name" /><br>
phone: <input type="text" v-model="user.phone" /><br>
<button @click="add">add user</button>
<hr>
users: {{ users }}
</div>

Why did it not work?

因为当你这样做时:

this.users.push( this.user );
// this changes the name of the user that happens to be inside the users array
this.user.name = '';

您正在将 this.user 添加到 this.users 数组中。如果您稍后更改它,例如 this.user.name = 'something';,您将更改同一个对象(现在也在 this.users 数组中)。

另一方面,覆盖它时,它是一个新对象:

this.users.push(this.user);
this.user = {name: '', phone: ''};
// the line above makes `this.user` point to (reference) a new object.
// the object that was pushed into the array still exists, but this.user does not point to it anymore
// the line below sets the name of the user created in the line above, not the previous (that is in the array)
this.user.name = 'bob';

备选方案:克隆。

如果你想走这条路,你会有一些选择。来自“手动”克隆:

new Vue({
el: '#app',
data: {
user: {name: '', phone: ''},
users: []
},
methods: {
add() {
this.users.push({name: this.user.name, phone: this.user.phone});
this.user.name = '';
this.user.phone = '';
}
}
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
name: <input type="text" v-model="user.name" /><br>
phone: <input type="text" v-model="user.phone" /><br>
<button @click="add">add user</button>
<hr>
users: {{ users }}
</div>

到深度克隆:

new Vue({
el: '#app',
data: {
user: {name: '', phone: ''},
users: []
},
methods: {
add() {
let userDeepClone = JSON.parse(JSON.stringify(this.user));
this.users.push(userDeepClone);
this.user.name = '';
this.user.phone = '';
}
}
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
name: <input type="text" v-model="user.name" /><br>
phone: <input type="text" v-model="user.phone" /><br>
<button @click="add">add user</button>
<hr>
users: {{ users }}
</div>

到浅克隆:

new Vue({
el: '#app',
data: {
user: {name: '', phone: ''},
users: []
},
methods: {
add() {
let userShallowClone = {...this.user}; // or Object.assign({}, this.user);
this.users.push(userShallowClone);
this.user.name = '';
this.user.phone = '';
}
}
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
name: <input type="text" v-model="user.name" /><br>
phone: <input type="text" v-model="user.phone" /><br>
<button @click="add">add user</button>
<hr>
users: {{ users }}
</div>

关于javascript - 重置 v-model 而不更新其因变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com