• Vue 2 TodoList 案例


    组件化编码流程(通用)

    1.实现静态组件:抽取组件,使用组件实现静态页面的效果

    结构

     2.代码

    1.App.vue

    1. <template>
    2. <div id="appContainer">
    3. <TodoComponent/>
    4. div>
    5. template>
    6. <script>
    7. import TodoComponent from './components/TodoComponent.vue';
    8. export default {
    9. name: "App",
    10. components: {
    11. TodoComponent
    12. }
    13. };
    14. script>
    15. <style>
    16. #appContainer{
    17. display: flex;
    18. justify-content: center;
    19. }
    20. style>

    2.TodoComponent.vue

    1. <template>
    2. <div id="container">
    3. <div id="parent">
    4. <TodoHeader :addTodo="addTodo"/>
    5. <TodoList :todos="todos" />
    6. <TodoFooter :finish="finish" :all="all" :clear="clear" :doneAll="doneAll" />
    7. div>
    8. div>
    9. template>
    10. <script>
    11. import TodoFooter from "./TodoFooter.vue";
    12. import TodoHeader from "./TodoHeader.vue";
    13. import TodoList from "./TodoList.vue";
    14. export default {
    15. name: "TodoComponent",
    16. components: {
    17. TodoFooter,
    18. TodoHeader,
    19. TodoList,
    20. },
    21. data() {
    22. return {
    23. todos:[
    24. {id:'001',title:'吃饭',done:true},
    25. {id:'002',title:'睡觉',done:false},
    26. {id:'003',title:'喝酒',done:true},
    27. {id:'004',title:'开车',done:true},
    28. {id:'005',title:'打游戏',done:false},
    29. {id:'006',title:'随便',done:false},
    30. {id:'007',title:'创业',done:false},
    31. ]
    32. }
    33. },
    34. methods:{
    35. addTodo(x){
    36. console.log("我是Todo组件,我收到了数据",x);
    37. this.todos.unshift(x);
    38. },
    39. clear(){
    40. console.log('清除了');
    41. const idList = this.todos.filter(x=>x.done).map(x=>x.id);
    42. for(let i=0;i<this.todos.length;i++){
    43. if(idList.includes(this.todos[i].id)){
    44. this.todos.splice(i,1);
    45. i--;
    46. }
    47. }
    48. console.log(this.todos);
    49. },
    50. doneAll(choose){
    51. this.todos.forEach(x=>x.done=choose)
    52. }
    53. },
    54. computed:{
    55. finish(){
    56. return this.todos.filter(x=>x.done).length;
    57. },
    58. all(){
    59. return this.todos.length;
    60. }
    61. }
    62. };
    63. script>
    64. <style scoped>
    65. #parent {
    66. width: 600px;
    67. border: 1px solid #ccc;
    68. display: flex;
    69. justify-content: center; /* 水平居左 */
    70. align-items: center; /* 垂直居中 */
    71. flex-direction: column; /* 垂直排列子元素 */
    72. }
    73. #parent > *{
    74. margin: 10px 0px;
    75. }
    76. style>

     3.TodoFooter.vue

    1. <template>
    2. <div id="container" v-show="all>0">
    3. <div>
    4. <input type="checkbox" v-model="allChoose" @change="doneAll(allChoose)"/>
    5. <label>已完成{{finish}} / 全部{{all}}label>
    6. div>
    7. <button id="button" @click="clear">清除已完成的任务button>
    8. div>
    9. template>
    10. <script>
    11. export default {
    12. name: "TodoFooter",
    13. data() {
    14. return {
    15. allChoose:false
    16. }
    17. },
    18. updated() {
    19. if(this.finish ===this.all && this.all > 0){
    20. this.allChoose = true;
    21. }else if(this.finish < this.all && this.all > 0){
    22. this.allChoose = false;
    23. }
    24. //双向绑定可以绑定 计算属性 前提是计算属性要写 setter
    25. },
    26. props:['finish','all','clear','doneAll']
    27. };
    28. script>
    29. <style scoped>
    30. #container {
    31. display: flex;
    32. justify-content: space-between; /* 水平居左 */
    33. width: 500px;
    34. height: 30px;
    35. padding: 10px;
    36. }
    37. #container *{
    38. padding: 5px;
    39. }
    40. #button {
    41. width: 140px;
    42. height: 28px;
    43. color: white;
    44. background-color: #eb1212;
    45. border: 1px solid #ccc;
    46. border-radius: 5px;
    47. padding: 5px;
    48. margin-right: 15px;
    49. cursor: pointer; /* 鼠标悬停时显示手型光标 */
    50. transition: background-color 0.3s ease; /* 平滑过渡 */
    51. }
    52. #button:hover {
    53. background-color: #d10d0d; /* 鼠标悬停时颜色变化 */
    54. }
    55. #button:active {
    56. background-color: #a00; /* 按下时颜色变化 */
    57. transform: translateY(2px); /* 按下时按钮下移效果 */
    58. }
    59. style>

    4.TodoHeader.vue

    1. <template>
    2. <div>
    3. <input
    4. id="headerInput"
    5. type="text"
    6. placeholder="请输入你的任务名称,按回车键确认"
    7. v-model="title"
    8. @keyup.enter="submit"
    9. />
    10. div>
    11. template>
    12. <script>
    13. import { nanoid } from "nanoid"; //函数
    14. export default {
    15. name: "TodoHeader",
    16. data() {
    17. return {
    18. title: "",
    19. };
    20. },
    21. props: ["addTodo"],
    22. methods: {
    23. submit() {
    24. //将用户的输入包装成一个todo对象
    25. if (this.title.trim() === "") {
    26. alert("输入不能为空");
    27. return;
    28. }
    29. const todoObj = {
    30. id: nanoid(),
    31. title: this.title,
    32. done: false,
    33. };
    34. this.addTodo(todoObj);
    35. this.title = "";
    36. //安装一些 nanoid npm i nanoid
    37. },
    38. },
    39. };
    40. script>
    41. <style scoped>
    42. #headerInput {
    43. width: 498px;
    44. height: 30px;
    45. border: 1px solid #ccc;
    46. border-radius: 2px;
    47. padding-left: 10px;
    48. }
    49. style>

     5.TodoItem.vue

    1. <template>
    2. <div id="container" @mouseover="handleMouseOver" @mouseleave="handleMouseLeave" >
    3. <div id="d" >
    4. <input type="checkbox" v-model="todo.done" />
    5. <span @click="info">{{ todo.title }}span>
    6. div>
    7. <button id="button" v-show="showDelete" @click="deleteTodo">删除button>
    8. div>
    9. template>
    10. <script>
    11. export default {
    12. name: "TodoItem",
    13. data() {
    14. return {
    15. todo: this.todoObj,
    16. showDelete: false,
    17. };
    18. },
    19. methods: {
    20. info() {
    21. console.log(this.todo);
    22. },
    23. handleMouseOver() {
    24. this.showDelete = true;
    25. },
    26. handleMouseLeave() {
    27. this.showDelete = false;
    28. },
    29. deleteTodo(){
    30. this.removeTodo(this.todoObj.id)
    31. }
    32. },
    33. props: {
    34. todoObj: {
    35. //传入的若是引用对象 则里面修改了,外面同样生效
    36. type: Object,
    37. required: true,
    38. },
    39. removeTodo:{
    40. type:Function,
    41. }
    42. },
    43. };
    44. script>
    45. <style scoped>
    46. #container:hover{
    47. background-color: #a09a9a;
    48. }
    49. #container {
    50. display: flex;
    51. justify-content: space-between; /* 水平居左 */
    52. align-items: center; /* 垂直居中 */
    53. width: 500px;
    54. height: 30px;
    55. border: 0.1px solid #ccc;
    56. border-radius: 2px;
    57. padding-left: 10px;
    58. padding-top: 5px;
    59. padding-bottom: 5px;
    60. }
    61. #container * {
    62. padding: 5px;
    63. }
    64. #button {
    65. width: 50px;
    66. height: 28px;
    67. color: white;
    68. background-color: #eb1212;
    69. border: 1px solid #ccc;
    70. border-radius: 5px;
    71. padding: 5px;
    72. margin-right: 15px;
    73. cursor: pointer; /* 鼠标悬停时显示手型光标 */
    74. transition: background-color 0.3s ease; /* 平滑过渡 */
    75. }
    76. #button:hover {
    77. background-color: #d10d0d; /* 鼠标悬停时颜色变化 */
    78. }
    79. #button:active {
    80. background-color: #a00; /* 按下时颜色变化 */
    81. transform: translateY(2px); /* 按下时按钮下移效果 */
    82. }
    83. style>

    6. TodoList.vue

    1. <template>
    2. <div>
    3. <TodoItem v-for="item in todosList" :key="item.id" :todoObj="item" :removeTodo="deleteTodo" />
    4. div>
    5. template>
    6. <script>
    7. import TodoItem from './TodoItem.vue'
    8. export default {
    9. name:'TodoList',
    10. components:{
    11. TodoItem
    12. },
    13. data() {
    14. return {
    15. todosList: this.todos
    16. }
    17. },
    18. methods:{
    19. deleteTodo(id){
    20. for(var i=0;i<this.todosList.length;i++){
    21. if(this.todosList[i].id===id){
    22. console.log(i,"下标");
    23. break;
    24. }
    25. }
    26. this.todosList.splice(i,1);
    27. }
    28. },
    29. props:['todos']
    30. }
    31. script>
    32. <style scoped>
    33. style>

  • 相关阅读:
    数据结构—List集合
    【Git LFS】huggingface 断点续传
    ZCMU--1431: Epic Game(C语言)
    论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
    Perl 中的模式匹配修饰符
    弄懂Rust编程中的Trait
    vmware: 磁盘加载问题导致,emergency mode: login incorrect 滚动打印
    pyinstaller 操作以及常见问题解决
    只基于html应用
    印度网络安全:威胁与应对
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/140965761