vue2 todolist案例

 

1,首先利用vue脚手架@vue/cli@4.5.13,创建vue2 项目 (安装过程有发布,可去查看)

2,配置ESLint:

解决ESLint和pretties之间冲突:关于方法后面是否需要空格问题(避免麻烦)

    'space-before-function-paren':'off'

3,下载dayjs库

npm i dayjs -S

4,在src目录下创建自定义插件(plugins.js):

(1)实现全局过滤器:用于时间格式化

(2)实现自定义指令:输入框自动获取焦点

import dayjs from 'dayjs'
export default{
  install(Vue){
        Vue.filter('format',function(val,time){
          return dayjs(val).format(time)
        })
        Vue.directive('fbind',{
          bind(ele,binding){
            ele.value=binding.value
          },
          inserted(ele,binding){
            ele.focus()
          },
          update(ele,binding){
            ele.value=binding.value
          }
        })
  }
}

如图

5,在main.js文件中引入这个文件(plugins.js)

import plugins from '@/plugins.js'
Vue.use(plugins)

 6,利用element-UI库实现静态页面,拆分成四个组件:

(1)实现列表数据渲染 (实现时间格式化)效果图如下:

src文件夹的App.vue文件

<template>
  <div id="app">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader />
        <MyList :todos="todos" />
        <MyFooter />
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from '@/components/MyHeader.vue'
import MyList from '@/components/MyList.vue'
import MyFooter from '@/components/MyFooter.vue'
export default {
  name: 'App',
  data() {
    return {
      todos: [
        { id: '001', title: '抽烟', time: Date.now(), done: false },
        { id: '002', title: '喝酒', time: Date.now(), done: true },
        { id: '003', title: '烫头', time: Date.now(), done: false },
        { id: '004', title: '学习', time: Date.now(), done: true }
      ]
    }
  },
  components: { MyHeader, MyList, MyFooter },
  methods: {}
}
</script>

<style lang="less">
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>

 在src文件夹的components文件夹的Myheader.vue文件

<template>
  <div class="todo-header">
    <el-input placeholder="请输入你的任务名称,按回车键确认"></el-input>
  </div>
</template>
<script>
export default {
  name: 'MyHeader',
  components: {},
  mixins: [],
  props: {},
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以参考以下代码实现一个简单的 Vue TodoList: ``` <template> <div class="todo-list"> <h1>Vue TodoList</h1> <form @submit.prevent="addItem"> <input type="text" v-model="newItem" placeholder="Add item..."> <button type="submit">Add</button> </form> <ul> <li v-for="(item, index) in items" :key="index"> <span>{{ item }}</span> <button @click="deleteItem(index)">Delete</button> </li> </ul> </div> </template> <script> export default { data() { return { newItem: '', items: [] } }, methods: { addItem() { if (this.newItem !== '') { this.items.push(this.newItem); this.newItem = ''; } }, deleteItem(index) { this.items.splice(index, 1); } } } </script> <style> .todo-list { margin: 0 auto; max-width: 600px; } ul { list-style: none; margin: 0; padding: 0; } li { display: flex; justify-content: space-between; align-items: center; margin: 10px 0; padding: 10px; border: 1px solid #ccc; border-radius: 5px; } button { background-color: #f44336; color: #fff; border: none; border-radius: 5px; padding: 5px 10px; cursor: pointer; } button:hover { background-color: #d32f2f; } input[type="text"] { padding: 10px; border-radius: 5px; border: 1px solid #ccc; margin-right: 10px; font-size: 16px; } </style> ``` 在这个 TodoList 中,我们使用了 `v-model` 指令绑定 `newItem` 变量,通过 `addItem` 方法向 `items` 数组中添加新的项目,并使用 `v-for` 指令渲染出每个项目。同时,在每个项目中,我们添加了一个 `Delete` 按钮,用于删除对应的项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值