首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重置v-模型而不更新其因变量。

问重置v-模型而不更新其因变量。
EN

Stack Overflow用户
提问于 2018-04-07 22:15:46
回答 1查看 4.2K关注 0票数 1

我有类似的<input>字段

代码语言:javascript
复制
<input type="text" v-model=user.name" />
<input type="text" v-model="user.phone" />
<button @click="add">add user</button>

每当我单击add user时,它都应该将user推到users数组并清除v-model (以推动更多的用户)。我的add()方法如下所示。

代码语言:javascript
复制
add()
{
  this.users.push( this.user );
  this.user.name = '';
  this.user.phone = '';
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-07 22:39:31

最简单的方法是重置整个user对象,而不是按属性设置属性:

代码语言:javascript
复制
add()
{
  this.users.push( this.user );
  this.user = {name: '', phone: ''};
}

演示:

​

代码语言:javascript
复制
new Vue({
  el: '#app',
  data: {
    user: {name: '', phone: ''},
    users: []
  },
  methods: {
    add() {
      this.users.push(this.user);
      this.user = {name: '', phone: ''};
    }
  }
})
代码语言:javascript
复制
<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
复制
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数组中)。

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

代码语言:javascript
复制
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';

替代方案:克隆。

如果你想走这条路,你会有其他选择的。来自“手工”克隆:

​

代码语言:javascript
复制
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 = '';
    }
  }
})
代码语言:javascript
复制
<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
复制
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 = '';
    }
  }
})
代码语言:javascript
复制
<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
复制
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 = '';
    }
  }
})
代码语言:javascript
复制
<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>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49712472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档