(Vue案例)TodoList

实现效果

 代码展示

main.js

//引入vue
import Vue from "vue";
//引入组件App
import App from "./App.vue";

//关闭vue的生产提示
Vue.config.productionTip = false;

//创建vm
new Vue({
  el: "#app",
  render: (h) => h(App),
});

App.vue

<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <todo-head :addTodo="addTodo" />
        <todo-list
          :todoList="todoList"
          :deleteTodo="deleteTodo"
          :updateFlag="updateFlag"
        />
        <todo-fotter
          :todoList="todoList"
          :updateFlagAll="updateFlagAll"
          :deleteFlagAll="deleteFlagAll"
        />
      </div>
    </div>
  </div>
</template>

<script>
import TodoList from "./components/todo-list.vue";
import TodoHead from "./components/todo-head.vue";
import TodoFotter from "./components/todo-fotter.vue";
//引入组件

export default {
  components: { TodoHead, TodoList, TodoFotter },
  name: "App",
  data() {
    return {
      todoList: [
        { id: "001", name: "yb", flag: true },
        { id: "002", name: "yg", flag: false },
        { id: "003", name: "yf", flag: true },
      ],
    };
  },
  methods: {
    //添加
    addTodo(todo) {
      this.todoList.unshift(todo); //在数组最前方添加
    },
    //删除
    deleteTodo(id) {
      //过滤出 不是要删除id的 所有值
      this.todoList = this.todoList.filter((todo) => todo.id != id);
    },
    //删除全部已选的
    deleteFlagAll() {
      this.todoList = this.todoList.filter((todo) => todo.flag === false);
    },
    //修改指定事 是否完成
    updateFlag(id) {
      this.todoList.forEach((todo) => {
        if (todo.id === id) todo.flag = !todo.flag;
      });
    },
    //修改全部的 是否完成
    updateFlagAll(flag) {
      this.todoList.forEach((todo) => {
        todo.flag = flag;
      });
    },
    //删除指定的
  },
};
</script>

<style>
/*base*/
body {
  background: plum;
}
.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>

components里的 todo-head.vue

<template>
  <div class="todo-header">
    <input
      type="text"
      placeholder="请输入你的任务名称,按回车键确认"
      @keyup.enter="add"
      v-model="todoName"
    />
  </div>
</template>

<script scoped>
//引入nanoid
import { nanoid } from "nanoid";
export default {
  props: ["addTodo"],
  data() {
    return {
      todoName: "",
    };
  },
  methods: {
    add() {
      //去除前后空格判断不为空 进行添加
      if (this.todoName.trim() != "" && this.todoName.trim() != null) {
        const todo = { id: nanoid(), name: this.todoName, flag: false };
        this.addTodo(todo);
        this.todoName = "";
      }
    },
  },
};
</script>

<style>
/*header*/
.todo-header input {
  width: 560px;
  height: 28px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 7px;
}

.todo-header input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
    0 0 8px rgba(82, 168, 236, 0.6);
}
</style>

components里的todo-list.vue

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以参考以下代码实现一个简单的 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` 按钮,用于删除对应的项目。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XinZeBig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值