JavaScript

2022/6/4 1:20:08

本文主要是介绍JavaScript,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

<script>

const app = Vue.createApp({
data() {
return {
userInformation: [
{ userName: '用户1', selected: '男', age: '18', phone: '12345678911'},
{ userName: '用户2', selected: '女', age: '19', phone: '12345678912'},
{ userName: '用户3', selected: '男', age: '20', phone: '12345678913'},
],
newInformation:{
userName:'',
selected:'男',
age:'',
phone:''
}
}
},
methods: {
// 创建新用户
createUser() {
// 1、添加用户信息判空
const { userName,selected,age,phone } = this.newInformation
if(!userName || !selected || !age || !phone) {
alert('输入信息不能为空')
// 不执行后面代码
return
}
// 2、添加新用户
this.userInformation.unshift(this.newInformation)
// 3、清空输入框信息 保证添加完用户信息双向绑定的数据修改,下方表格数据不会随着修改
this.newInformation = {}

},
//删除功能
remove(index) {
// splice() 数组的删除方法 第一个参数是删除的位置 第二个参数是删除的个数
// 这边remove方法传值的index指的就是 userInformation 数组里面的索引值
this.userInformation.splice(index,1)
}
},
computed: {

}
}).mount('#app');

</script>



这篇关于JavaScript的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程