当前位置:   article > 正文

基于vue的todolist案例_todolist的styles

todolist的styles

前言:todolist案例真的算是经典了,不管你是学习原生js,还是学习jq,还是学习vue,亦或者是react,todolist是练习必不可少的练习demo了,下面我们来看看这个案例。需要完整代码的小朋友戳这里获取资料https://download.csdn.net/download/TroyeSivanlp/60226570

项目展示

请添加图片描述

技术要点

  1. 使用组件通信(父传子,子传父)
  2. 考验对v-for的使用熟练度
  3. 组件模块化划分
  4. vue组件-scoped作用
  5. webstorage的使用

项目功能

  1. 添加任务
  2. 删除任务
  3. 底部栏的统计任务
  4. 底部栏的已完成的任务和未完成的任务切换
  5. 清除已完成的任务

案例骨架

首先,创建项目骨架。

  1. 首先创建一个项目demo,名叫作todolist。
  2. 然后再components文件夹下创建TodoList.vue,TodoMain.vue,TodoFooter.vue
  3. 系统默认是不会创建vue.config,js这个文件的,如果想要关闭eslint,就创建这个文件,里面写这个代码(如果熟悉eslint报错的,不限这个报错麻烦的这一步可以不用设置)
module.exports = {
    devServer: { // 自定义服务配置
      open: true,
      port: 3000
    },
    lintOnSave: false // 关闭eslint检查
  }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 把提前准备的styles样式拖过来,
  2. 然后项目初始搭建就完成了

在这里插入图片描述

项目具体的代码分析

App.vue中的代码

<template>
  <div class="todoapp">
    <TodoHeader :arr="list" @create="createFn"></TodoHeader>
    <TodoMain :arr="showArr" @del="deleteFn"></TodoMain>
    <TodoFooter :farr="showArr" @changeType="typeFn"
    @clear="clearFun"
    ></TodoFooter>
  </div>
</template>

<script>
// 1. 目标: 创建工程-准备相关组件文件-标签+样式 (静态)
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
// 1.1 组件引入
import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'
export default {
  // 1.2 组件注册
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  // 2. 目标: 铺设待办任务
  // 2.0 - 准备数据
  data(){
    return {
      // 8.1 默认从本地取值
      list: JSON.parse(localStorage.getItem('todoList')) || [],
      // 6.4 先中转接收类型字符串
      getSel: "all" // 默认显示全部
    }
  },
  methods: {
    createFn(taskName){ // 添加任务
      // 3.3 push到数组里
      let id = this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1
      this.list.push({
        id: id,
        name: taskName,
        isDone: false
      })
    },
    deleteFn(theId){ // 删除任务
      let index = this.list.findIndex(obj => obj.id === theId)
      this.list.splice(index, 1)
    },
    typeFn(str){ // 'all' 'yes' 'no' // 修改类型
      this.getSel = str
    },
    clearFun(){ // 清除已完成
      this.list = this.list.filter(obj => obj.isDone == false)
    }
  },
  // 6.5 定义showArr数组 - 通过list配合条件筛选而来
  computed: {
    showArr(){
      if (this.getSel === 'yes') { // 显示已完成
        return this.list.filter(obj => obj.isDone === true)
      } else if (this.getSel === 'no') { // 显示未完成
        return this.list.filter(obj => obj.isDone === false)
      } else {
        return this.list // 全部显示
      }
    }
  },
  // 8. 目标: 数据缓存
  watch: {
    list: {
      deep: true,
      handler(){
        // 8.0 只要list变化 - 覆盖式保存到localStorage里
        localStorage.setItem('todoList', JSON.stringify(this.list))
      }
    }
  }
}
</script>

<style>

</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

TodoHeader.vue中的代码

<template>
  <header class="header">
    <h1>todos</h1>
    <!-- 9. 目标: 全选状态
      9.0 v-model关联全选状态
      页面变化(勾选true, 未勾选false) -> v-model -> isAll变量
     -->
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" />
    <label for="toggle-all"></label>
    <!-- 3.0 键盘事件-回车按键
         3.1 输入框 - v-model获取值
     -->
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      @keydown.enter="downFn"
      v-model="task"
    />
  </header>
</template>

<script>
// 3. 目标 - 新增任务
export default {
  data() {
    return {
      task: "",
    };
  },
  methods: {
    downFn() {
      if (this.task.trim().length === 0) {
        alert("任务名不能为空");
        return;
      }
      // 3.2(重要) - 当前任务名字要加到list数组里
      // 子传父技术
      this.$emit("create", this.task);
      this.task = "";
    },
  },
  // 9.1 定义计算属性
  computed: {
    isAll: {
      set(checked) {
        // 只有true / false
        // 9.3 影响数组里每个小选框绑定的isDone属性
        this.arr.forEach((obj) => (obj.isDone = checked));
      },
      get() {
        // 9.4 小选框统计状态 -> 全选框
        // 9.5 如果没有数据, 直接返回false-不要让全选勾选状态
        return (
          this.arr.length !== 0 && this.arr.every((obj) => obj.isDone === true)
        );
      },
    },
  },
  // 9.2 父 -> 子 list数组
  props: ["arr"],
};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

TodoMain.vue中的代码

<template>
  <ul class="todo-list">
    <!-- 2.2 循环任务-关联选中状态-铺设数据 -->
    <!-- completed: 完成的类名 -->
    <li :class="{completed: obj.isDone}" v-for="obj in arr" :key='obj.id'>
      <div class="view">
        <input class="toggle" type="checkbox" v-model="obj.isDone"/>
        <label>{{ obj.name }}</label>
        <!-- 4.0 注册点击事件 -->
        <button class="destroy" @click="delFn(obj.id)"></button>
      </div>
    </li>
  </ul>
</template>

<script>
// 4. 目标: 删除list里数据
export default {
  // 2.1 定义props
  props: ['arr'],
  methods: {
    delFn(id){
      // 4.1 子传父
      this.$emit('del', id)
    }
  }
}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

TodoFooter.vue中的代码

<template>
  <footer class="footer">
    <span class="todo-count">剩余<strong>{{ count }}</strong></span>
    <ul class="filters" @click="fn">
      <li>
        <!-- 6.1 判断谁应该有高亮样式: 动态class
            6.2 用户点击要切换isSel里保存的值
         -->
        <a :class="{selected: isSel === 'all'}" href="javascript:;" @click="isSel='all'">全部</a>
      </li>
      <li>
        <a :class="{selected: isSel === 'no'}" href="javascript:;" @click="isSel='no'">未完成</a>
      </li>
      <li>
        <a :class="{selected: isSel === 'yes'}" href="javascript:;" @click="isSel='yes'">已完成</a>
      </li>
    </ul>
    <!-- 7. 目标: 清除已完成 -->
    <!-- 7.0 点击事件 -->
    <button class="clear-completed" @click="clearFn">清除已完成</button>
  </footer>
</template>

<script>
// 5. 目标: 数量统计
export default {
  // 5.0 props定义
  props: ['farr'],
  // 5.1 计算属性 - 任务数量
  computed: {
    count(){
      return this.farr.length
    }
  },
  // 6. 目标: 点谁谁亮
  // 6.0 变量isSel
  data(){
    return {
      isSel: 'all' // 全部:'all', 已完成'yes', 未完成'no'
    }
  },
  methods: {
    fn(){ // 切换筛选条件
      // 6.3 子 -> 父 把类型字符串传给App.vue 
      this.$emit("changeType", this.isSel)
    },
    clearFn(){ // 清空已完成任务
      // 7.1 触发App.vue里事件对应clearFun方法
      this.$emit('clear')
    }
  }
}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/223893
推荐阅读
相关标签
  

闽ICP备14008679号