Vue

快速使用Vue2实现记事本

2023-08-08  本文已影响0人  h2coder
记事本.png

项目地址

WebNodepad

需求分析

布局分析

需求拆解

组件拆分

从上面的分析得知,组件要分拆为4个

代码时间

根组件(App.vue)

<template>
  <section id="app">
    <!-- 头部 -->
    <TodoHeader @add="onAdd"></TodoHeader>
    <!-- 身体 -->
    <TodoMain :list="list" @delete="onDelete"></TodoMain>
    <!-- 尾部 -->
    <TodoFooter :list="list" @clear="onClear"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

// 本地存储的Key
const dataKey = "xh-todos";

// 默认数据
const defaultList = [
  { id: 1, name: "打篮球" },
  { id: 2, name: "看电影" },
  { id: 3, name: "逛街" },
];

export default {
  data() {
    return {
      // 优先从本地存储中,获取数据
      list: JSON.parse(localStorage.getItem(dataKey)) || defaultList,
    };
  },
  methods: {
    // 处理添加
    onAdd(todoName) {
      this.list.unshift({
        id: Date.now(),
        name: todoName,
      });
    },
    // 处理删除
    onDelete(id) {
      const index = this.list.findIndex((item) => item.id === id);
      this.list.splice(index, 1);
    },
    // 清空
    onClear() {
      this.list = [];
    },
  },
  watch: {
    list: {
      // 深度监听
      deep: true,
      handler(newValue) {
        // 同步数据到本地存储
        localStorage.setItem(dataKey, JSON.stringify(newValue));
      },
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>

<style>
</style>

头部组件(TodoHeader.vue)

<template>
  <!-- 输入框 -->
  <header class="header">
    <h1>记事本</h1>
    <input
      @keyup.enter="add"
      v-model.trim="todoName"
      placeholder="请输入任务"
      class="new-todo"
    />
    <button @click="add" class="add">添加任务</button>
  </header>
</template>

<script>
export default {
  data() {
    return {
      // 输入框的文本
      todoName: "",
    };
  },
  methods: {
    // 添加
    add() {
      if (this.todoName === "") {
        return;
      }
      // 发送事件,让父组件,添加数据到数组中
      this.$emit("add", this.todoName);
      // 清空输入框
      this.todoName = "";
    },
  },
};
</script>

<style>
</style>

尾部(TodoFooter.vue)

<template>
  <!-- 统计和清空 -->
  <footer class="footer">
    <!-- 统计 -->
    <span class="todo-count"
      >合 计:<strong> {{ list.length }} </strong></span
    >
    <!-- 清空 -->
    <button @click="$emit('clear')" class="clear-completed">清空任务</button>
  </footer>
</template>

<script>
export default {
  props: ["list"],
};
</script>

<style>
</style>
上一篇 下一篇

猜你喜欢

热点阅读