使用Vue做一个简单的todo应用的三种方式的示例代码


Posted in Javascript onOctober 20, 2018

1. 引用vue.js

<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
 <div id="root">
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">提交</button>
  <ul>
   <li 
     v-for="(item,index) of lists" 
     :key="index" 
     @click="handlerDel(index)"
    >
    {{item}}
   </li>
  </ul>
 </div>
 
 <script>
  new Vue({
   el: '#root',
   data: {
    inputValue: '',
    lists: []
   },
   methods: {
    handlerAdd: function() {
     this.lists.push(this.inputValue);
     this.inputValue = '';
    },
    handlerDel: function(index) {
     this.lists.splice(index, 1);
    }
   }
  });
 </script>
</body>
</html>

2. 全局组件注册

<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
 <div id="root">
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">提交</button>
  <ul>
   <todo-item
    v-for="(item,index) of lists"
    :content = "item"
    :index = "index"
    :key = "index"
    @delete="handlerDel"
   >
   </todo-item>
  </ul>
 </div>
 
 <script>
  Vue.component('todoItem', {
   props: {
    content: String,
    index: Number
   },
   template: '<li @click="handlerClick">{{content}}</li>',
   methods: {
    handlerClick: function(){
     this.$emit('delete', this.index);
    }
   }

  });

  new Vue({
   el: '#root',
   data: {
    inputValue: '' ,
    lists: []
   },
   methods: {
    handlerAdd: function() {
     this.lists.push(this.inputValue);
     this.inputValue = '';
    },
    handlerDel: function(index) {
     this.lists.splice(index,1);
    }
   }
  });
 </script>
</body>
</html>

3. vue-cli脚手架

// Todo.Vue

<template>
 <div>
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">提交</button>
  <ul>
   <todo-item
    v-for="(item,index) of lists"
    :key="index"
    :content="item"
    :index="index"
    @delete="handlerDel"
   ></todo-item>
  </ul>
 </div>
</template>

<script>
import TodoItem from './components/todoItem'

export default {
 data () {
  return {
   inputValue: '',
   lists: []
  }
 },
 methods: {
  handlerAdd () {
   this.lists.push(this.inputValue)
   this.inputValue = ''
  },
  handlerDel (index) {
   this.lists.splice(index, 1)
  }
 },
 components: {
  'todo-item': TodoItem
 }
}
</script>

<style>

</style>
// TodoItem.vue

<template>
 <li @click="handlerClick" class="item">{{content}}</li>
</template>

<script>
export default {
 props: ['content', 'index'],
 methods: {
  handlerClick () {
   this.$emit('delete', this.index)
  }
 }
}
</script>

<style scoped>
 ul,li {
  list-style: none;
 }
 .item {
  color: blueviolet;
 }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
基于jQuery的弹出警告对话框美化插件(警告,确认和提示)
Jun 10 Javascript
利用jQuery接受和处理xml数据的代码(.net)
Mar 28 Javascript
推荐11款jQuery开发的复选框和单选框美化插件
Aug 02 Javascript
js document.write()使用介绍
Feb 21 Javascript
js遍历json对象所有key及根据动态key获取值的方法(必看)
Mar 09 Javascript
激动人心的 Angular HttpClient的源码解析
Jul 10 Javascript
简单实现js轮播图效果
Jul 14 Javascript
Node.js Windows Binary二进制文件安装方法
May 16 Javascript
JS利用prototype给类添加方法操作详解
Jun 21 Javascript
解决vue.js中settimeout遇到的问题(时间参数短效果不稳定)
Jul 21 Javascript
解决ant Design Search无法输入内容的问题
Oct 29 Javascript
JS实现页面侧边栏效果探究
Jan 08 Javascript
Iview Table组件中各种组件扩展的使用
Oct 20 #Javascript
详解webpack打包第三方类库的正确姿势
Oct 20 #Javascript
详解ES6 Promise对象then方法链式调用
Oct 20 #Javascript
Intellij IDEA搭建vue-cli项目的方法步骤
Oct 20 #Javascript
分享5个小技巧让你写出更好的 JavaScript 条件语句
Oct 20 #Javascript
angular4 获取wifi列表中文显示乱码问题的解决
Oct 20 #Javascript
vue 项目地址去掉 #的方法
Oct 20 #Javascript
You might like
PHP VS ASP
2006/10/09 PHP
PHP中array_merge和array相加的区别分析
2013/06/17 PHP
PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
2019/11/04 PHP
4种Windows系统下Laravel框架的开发环境安装及部署方法详解
2020/04/06 PHP
jquery ajax return没有返回值的解决方法
2011/10/20 Javascript
Prototype源码浅析 Enumerable部分之each方法
2012/01/16 Javascript
20行代码实现的一个CSS覆盖率测试脚本
2013/07/07 Javascript
js实现简单的购物车有图有代码
2014/05/26 Javascript
Javascript获取当前日期的农历日期代码
2014/10/08 Javascript
node.js中的fs.lchmodSync方法使用说明
2014/12/16 Javascript
AngularJS向后端ASP.NET API控制器上传文件
2016/02/03 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
jquery实现ajax提交表单信息的简单方法(推荐)
2016/08/24 Javascript
JS实现的几个常用算法
2016/11/12 Javascript
bootstrap table表格插件使用详解
2017/05/08 Javascript
AngularJS日程表案例详解
2017/08/15 Javascript
微信小程序实现动态改变view标签宽度和高度的方法【附demo源码下载】
2017/12/05 Javascript
React中嵌套组件与被嵌套组件的通信过程
2018/07/11 Javascript
node版本管理工具n包使用教程详解
2018/11/09 Javascript
详解如何理解vue的key属性
2019/04/14 Javascript
layui富文本编辑器前端无法取值的解决方法
2019/09/18 Javascript
浅谈vue中组件绑定事件时是否加.native
2019/11/09 Javascript
vue实现桌面向网页拖动文件的示例代码(可显示图片/音频/视频)
2021/03/01 Vue.js
简单介绍Python中的JSON模块
2015/04/08 Python
结合Python的SimpleHTTPServer源码来解析socket通信
2016/06/27 Python
Python编程scoketServer实现多线程同步实例代码
2018/01/29 Python
Python3中详解fabfile的编写
2018/06/24 Python
python模块导入的细节详解
2018/12/10 Python
Python中字符串List按照长度排序
2019/07/01 Python
Python实现把多维数组展开成DataFrame
2019/11/30 Python
CSS3 border-radius圆角的实现方法及用法详解
2020/09/14 HTML / CSS
伯克斯奥特莱斯:Burkes Outlet
2019/03/30 全球购物
怎样拟定创业计划书
2014/05/01 职场文书
领导干部对照检查材料
2014/08/24 职场文书
2015年行风建设工作总结
2015/05/15 职场文书
六五普法心得体会2016
2016/01/21 职场文书