【发布时间】:2019-10-17 15:05:01
【问题描述】:
我正在尝试使用 Vue 制作动态表单。用户单击添加按钮,会出现另一行供他们添加数据。问题是,我最初只设置了 1 行,但是当我将 v-for 放在 div 上时,开始出现 3 额外的行。我很困惑为什么会这样?另外,当我单击添加按钮时,什么也没有发生。谢谢。
<div class="container">
<title>exportDATA | Web to Excel</title>
<div class="mt-4">
<div class="jumbotron bg-dark col-md-10 offset-md-1">
<div class="card card-body bg-dark" v-for="(user,index) in users" :key="index">
<form>
<div class="row">
<div class="form-group col-md-3">
<label for="email" class="text-white">Username:</label>
<input type="text" class="form-control" id="username" v-model="user.username">
</div>
<div class="form-group col-md-3">
<label for="pwd" class="text-white">Password:</label>
<input type="password" class="form-control" id="password" v-model="user.password">
</div>
<div class="form-group col-md-3">
<label for="pwd" class="text-white">Phone Number:</label>
<input type="text" class="form-control" id="phone" v-model="user.phone">
</div>
<div class="form-group col-md-3">
<label for="pwd" class="text-white">Email</label>
<input type="email" class="form-control" id="email" v-model="user.email">
</div>
</div>
</form>
</div>
<br>
<button class="btn btn-secondary float-right" title="Click to add row"><span class="fa fa-plus-square" @click="addMoreData(index)" v-show="index == users.length-1"></span> Add More Data</button>
</div>
<div class="jumbotron bg-dark col-md-10 offset-md-1 ">
<button class="btn btn-success col-md-6 offset-md-3 p-3"><span class="fa fa-plus-square"></span> Export to Microsoft Excel</button>
</div>
</div>
</div>
脚本
export default {
data(){
return{
users:{
username:'',
password:'',
email:'',
phone:''
}
}
},
methods:{
addMoreData(index){
this.users.push({
username: '',
password:'',
email:'',
phone:''
});
}
},
mounted() {
console.log('Component mounted.')
}
}
【问题讨论】:
-
users 是一个“对象”,而不是一个数组 - 在“addMoreData()”中推送是不正确的
标签: javascript forms vue.js