vue基础:将表单中的数据按列表显示在页面下,删除数据this.list.push(***),this.list.push(***)。

将表单中的数据按列表显示在页面下,删除数据

1.添加数据

在这里插入图片描述

1.在页面中,首先通过数据的双向绑定,表单中的v-model=“todo”,绑定数据。
2.通过调用@click="doadd()"方法中的this.list.push(this.todo),将todo中的数据加到list中。
3.通过循环列表显示。

<template>
  <div id="app">
    <input type="text" v-model="todo"/>
    <button @click="doadd()">增加</button>
    <br>
    <br>
    <ul>
    <li v-for="a in list">
      {{a}}
    </li>
  </ul>
  </div>
</template>

<script>
export default {
  data(){
    return{
      todo:'',
      list:[]
    }
  },
  methods:{
    doadd(){
      this.list.push(this.todo)
      this.todo=''
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
2.删除添加的数据

在这里插入图片描述

<template>
  <div id="app">
    <input type="text" v-model="todo"/>
    <button @click="doadd()">增加</button>
    <br>
    <br>
    <ul>
    <li v-for="(aa,key) in list">
      {{aa}}--<button @click="dodel(key)">删除</button>
    </li>
  </ul>
  </div>
</template>

<script>
export default {
  data(){
    return{
      todo:'',
      list:[]
    }
  },
  methods:{
    doadd(){
      this.list.push(this.todo)
      this.todo=''
    },
    dodel(key){
      // alert(key)
      this.list.splice(key,1)
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

1.在添加中我们用了this.list.push(this.todo),因此删除就有this.list.push(this.todo)。
2.需要注意的是在删除时,我们要获取每条数据的标号,才能对应删除,所以在比那里列表的时候,就不单单是输出值,也要表索引表示出来。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷咕咕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值