1. //先引入vue.js
  2. <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
  3. <!-- 1.数据驱动,不用频繁的操作DOM
  4. vue 属性如果要接收一个变量的时候前面要放置一个冒号 -->
  5. <div id="app">
  6. <p><input type="text" v-model="keyword"><button @click="handleApp">添加</button></p>
  7. <p>{{keyword}}</p>
  8. <ul>
  9. <li v-for="item of arr"
  10. :data-index="index"
  11. @click="handleDelete">{{item}} -{{index}}</li>
  12. </ul>
  13. </div>
  14. <script>
  15. new Vue({
  16. el:"#app",
  17. data:{
  18. arr:["小米","华为","苹果"],
  19. keyword:""
  20. },
  21. methods:{
  22. handleApp(){
  23. this.arr.push(this.keyword)
  24. },
  25. handleDelete(event){
  26. var index=event.target.dataset.index;
  27. this.arr.splice(index,1)
  28. }
  29. }
  30. })
  31. </script>

例子地址

https://gitee.com/wangsiman/wuhannotebook/blob/master/typescript/js-day05/08vue.html