尚硅谷—TodoList案例

本文详细介绍了在Vue.js中进行组件化编码的流程,包括如何拆分静态和动态组件,处理数据的位置,以及组件间的通信。强调了props的使用场景,特别是不能在v-model中绑定props的值,同时指出尽管可以修改props传递的对象属性,但不推荐这样做。文中还列举了具体的组件文件如header、list、item和footer。
1.组件化编码流程

​ 1.1 拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突

​ 1.2实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用

​ 1.2.1 一个组件在用:放在该组件的data中即可

​ 1.2.2 一些组件在:放在他们共同的父组件上

​ 1.3实现交互:从绑定事件开始

2.props适用于:

​ 2.1父组件——>子组件 通信

​ 2.2子组件——>父组件 通信(要求父组件先给子组件一个函数,然后子组件调用该函数,将数据当参数传给父元素)

3.使用v-model时要切记:v-model绑定的值不能是props传过来的值,因为props是不可以修改的
4.props传过来的若是对象类型的值,修改对象中的属性时,Vue不会报错,但不推荐这样做

header.vue

<template>
  <div class="todo-header">
    <input
      type="text"
      placeholder="请输入你的任务名称,按回车键确认"
      @keyup.enter="addItems"
      v-model="title"
    />
    <span v-if="flag" class="showtips">*内容不能为空</span>
  </div>
</template>

<script>
// nanoid:获取唯一的标识,是uuid的变种,轻量级的uuid
import { nanoid } from "nanoid";
export default {
  name: "SearchHeader",
  data() {
    return {
      title: "",
      flag: false,
    };
  },
  // 接收父组件传递来的数据(函数)
  props: ["addItem"],
  methods: {
    addItems() {
      // 效验数据
      // this.title.trim()
      if (!this.title.trim()) {
        this.flag = true;
        this.title = "";
        return;
      }
      this.flag = false;
      // 将用户的输入包装成一个对象
      // console.log(e.target.value);
      const obj = {
        id: nanoid(),
        title: this.title.trim(),
        done: false,
      };
      // console.log(obj);
      // 调用该函数,将添加的对象作为参数
      this.addItem(obj);
      // 清空输入框的内容
      this.title = "";
    },
  },
};
</script>
<style scoped>
.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), 0px, 0x, 8px,
    rgba(82, 168, 236, 0.6);
}
.showtips {
  font-size: 10px;
  color: red;
}
</style>

list.vue

<template>
  <div>
    <ul class="todo-main">
      <!-- 通过props属性,父子组件传值,将item传递给子组件 -->
      <SearchItem
        v-for="item in dataList"
        :key="item.id"
        :items="item"
        :changeStatus="changeStatus"
        :delItem="delItem"
      />
    </ul>
  </div>
</template>

<script>
import SearchItem from "./SearchItem.vue";

export default {
  name: "SearchList",
  components: {
    SearchItem,
  },
  props: ["dataList", "changeStatus", "delItem"],
};
</script>

<style scoped>
.todo-main {
  margin-left: 0;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding-left: 0;
  margin-top: 10px;
}
.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
</style>

item.vue

<template>
  <div>
    <li>
      <label for="">
        <!--方式一: @click="handler(items.id)" -->
        <input
          type="checkbox"
          :checked="items.done"
          @change="handler(items.id)"
        />
        <!-- 以下代码也能实现修改状态的功能,但是不建议用,因为有点违反原则,因为修改了props -->
        <!-- <input type="checkbox" v-model="items.done" /> -->
        <span>{{ items.title }}</span>
      </label>
      <button class="btn btn-danger" @click="handleDel(items.id)">删除</button>
    </li>
  </div>
</template>

<script>
export default {
  name: "SearchItem",
  data() {
    return {};
  },
  // 声明接收items对象
  props: ["items", "changeStatus", "delItem"],
  methods: {
    handler(id) {
      // console.log(id);
      // 将doList中对应的任务的状态改变
      this.changeStatus(id);
    },
    handleDel(id) {
      // console.log(id);
      if (confirm("确定删除该任务吗?")) {
        this.delItem(id);
      } else {
        return;
      }
    },
  },
};
</script>

<style scoped>
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}
li label {
  float: left;
  cursor: pointer;
}
li label input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}
li button {
  float: right;
  display: none;
  margin-top: 3px;
}
li:hover {
  background: #ddd;
}
li:hover button {
  display: block;
}
li:before {
  content: initials;
}
li:last-child {
  border-bottom: none;
}
</style>

footer.vue

<template>
  <div class="todo-footer">
    <label>
      <!-- 这个做法就需要用到dom元素 -->
      <!-- <input type="checkbox" :checked="isAll" @change="checkAll" /> -->
      <!-- checkbox的value值就是:true/false,双向绑定的做法用不到dom元素 -->
      <input type="checkbox" v-model="isAll" />
    </label>
    <span>已完成{{ doneTotal }} / 全部 {{ total }}</span>
    <button class="btn btn-danger" @click="handleDone">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "SearchFooter",
  props: ["doList", "checkedStatus", "delDone"],
  data() {
    return {};
  },
  methods: {
    // 全选全不选效果
    // 用函数的做法会使用到dom元素
    /* checkAll(e) {
      this.checkedStatus(e.target.checked);
    }, */
    // 清除已完成的任务
    handleDone() {
      this.delDone();
    },
  },
  computed: {
    // 已完成任务
    doneTotal() {
      /* let i = 0;
      // 遍历doList,
      this.doList.forEach((v) => {
        // 判断
        if (v.done) {
          i++;
        }
      });
      return i; */
      // reduce:累加器效果,数组有多长,就累加多少次
      // x 为reduce最后一个累加器之后的结果
      /* const x = this.doList.reduce((pre, current) => {
        return pre + (current.done ? 1 : 0);
      });
      return x; */
      return this.doList.reduce((pre, item) => {
        return pre + (item.done ? 1 : 0);
      }, 0);
    },
    // 总任务数
    total() {
      return this.doList.length;
    },
    // 全选
    /* isAll() {
      return this.doneTotal == this.total && this.total && this.total > 0;
    }, */
    // 原生双向绑定,这里就用不到dom元素
    isAll: {
      get() {
        return this.doneTotal == this.total && this.total && this.total > 0;
      },
      // 这里的value就是checkbox的结果:true/false
      set(value) {
        this.checkedStatus(value);
      },
    },
  },
};
</script>

<style scoped>
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}
.todo-footer label {
  display: inline-block;
  margin-right: 28px;
  cursor: pointer;
}
.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}
.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>

app.vue

<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <!-- 向子组件传递一个函数 -->
      <SearchHeader :addItem="addItem" />
      <SearchList
        :dataList="doList"
        :changeStatus="changeStatus"
        :delItem="delItem"
      />
      <SearchFooter
        :doList="doList"
        :checkedStatus="checkedStatus"
        :delDone="delDone"
      />
    </div>
  </div>
</template>

<script>
import SearchHeader from "./components/SearchHeader.vue";
import SearchFooter from "./components/SearchFooter.vue";
import SearchList from "./components/SearchList.vue";

export default {
  components: {
    SearchHeader,
    SearchFooter,
    SearchList,
  },
  data() {
    return {
      doList: [
        { id: "01", title: "吃饭", done: true },
        { id: "02", title: "睡觉", done: false },
        { id: "03", title: "打代码", done: true },
      ],
    };
  },
  methods: {
    // 子传父结合父传子
    // 添加新任务
    addItem(x) {
      // console.log("从header来的数据:" + x);
      // 在doList前面添加新任务
      this.doList.unshift(x);
      // console.log(this.doList);
    },
    // 改变任务状态
    changeStatus(id) {
      // 遍历doList
      this.doList.forEach((item) => {
        // 根据id查找对应的任务
        if (item.id == id) {
          // 改变状态
          item.done = !item.done;
        }
      });
    },
    // 删除指定的任务
    delItem(v) {
      this.doList = this.doList.filter((i) => {
        return i.id != v;
      });
    },
    // 改变任务状态
    checkedStatus(done) {
      this.doList.forEach((v) => {
        v.done = done;
      });
    },
    // 清除已完成的任务
    delDone() {
      // 返回过滤结果为true的数据
      this.doList = this.doList.filter((v) => {
        // 当done为true时,!true为false,结果为false,不能返回,直接淘汰
        return !v.done;
      });
    },
  },
};
</script>
<style >
/* 这里面的样式是所有组件共用的 */
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), 0px, 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>>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值