• Todolist案例vue写法


     App.vue

    1. <script>
    2. import TodoHeader from "./components/TodoHeader.vue";
    3. import TodoList from "./components/TodoList.vue";
    4. import TodoFooter from "./components/TodoFooter.vue";
    5. export default {
    6. name: "App",
    7. data() {
    8. return {
    9. todos: [
    10. { id: 1, name: "vue", done: true },
    11. { id: 2, name: "react", done: false },
    12. { id: 3, name: "html", done: true },
    13. { id: 4, name: "java", done: false },
    14. ],
    15. };
    16. },
    17. methods: {
    18. //添加 修改 删除数据 需要在app里面定义方法
    19. addTodo(name) {
    20. const item = { id: new Date().getTime(), name, done: false };
    21. this.todos.unshift(item);
    22. },
    23. delTodo(id) {
    24. this.todos = this.todos.filter((item) => {
    25. return item.id != id;
    26. });
    27. },
    28. //修改状态
    29. editDone(id) {
    30. this.todos.forEach((item) => {
    31. if (item.id == id) {
    32. item.done = !item.done;
    33. }
    34. });
    35. },
    36. //全选/全不选
    37. editAll(checked) {
    38. this.todos.forEach((item) => {
    39. item.done = checked;
    40. });
    41. },
    42. delAll() {
    43. this.todos = this.todos.filter((item) => {
    44. return item.done != true;
    45. });
    46. },
    47. },
    48. components: {
    49. TodoHeader,
    50. TodoList,
    51. TodoFooter,
    52. },
    53. };
    54. script>
    55. <style>
    56. * {
    57. margin: 0px;
    58. padding: 0px;
    59. }
    60. li {
    61. list-style: none;
    62. }
    63. .main {
    64. width: 600px;
    65. margin: auto;
    66. }
    67. .btn {
    68. position: absolute;
    69. right: 0px;
    70. background: red;
    71. color: #fff;
    72. width: 90px;
    73. height: 33px;
    74. border: none;
    75. border-radius: 5px;
    76. cursor: pointer;
    77. display: none;
    78. }
    79. /* list样式 */
    80. .list {
    81. border: 1px solid #999999;
    82. padding: 15px;
    83. margin-top: 15px;
    84. }
    85. .list li {
    86. height: 40px;
    87. line-height: 40px;
    88. color: #666;
    89. border-bottom: 1px solid #999999;
    90. position: relative;
    91. }
    92. .list li .btn {
    93. top: 2px;
    94. }
    95. .list li:hover .btn {
    96. display: block;
    97. }
    98. /* 底部 */
    99. .footer {
    100. border: 1px solid #999999;
    101. padding: 15px;
    102. margin-top: 15px;
    103. position: relative;
    104. }
    105. .footer .btn {
    106. display: block;
    107. padding: 10px 20px;
    108. width: auto;
    109. height: auto;
    110. padding-bottom: 9px;
    111. top: 6px;
    112. right: 5px;
    113. }
    114. style>

    main.js

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. Vue.config.productionTip = false
    4. new Vue({
    5. render: h => h(App),
    6. }).$mount('#app')

     TodoHeader.vue

    1. <script>
    2. export default {
    3. name:"TodoHeader",
    4. props:["addTodo"],
    5. methods: {
    6. pressAdd(e) {
    7. this.addTodo(e.target.value);
    8. }
    9. },
    10. }
    11. script>
    12. <style>
    13. /* 头部文件 */
    14. .header{
    15. border: 1px solid #999999;
    16. padding: 15px;
    17. }
    18. .header input{
    19. width: 100%;
    20. height: 40px;
    21. line-height: 40px;
    22. }
    23. style>

    TodoList.vue

    1. <script>
    2. import Item from './Item.vue';
    3. export default {
    4. name:"TodoList",
    5. props:["todos","delTodo","editDone"],
    6. mounted(){
    7. // console.log(this.todos);
    8. },
    9. components:{
    10. Item
    11. }
    12. }
    13. script>
    14. <style>
    15. style>

    TodoFooter.vue

    1. <script>
    2. export default {
    3. name:"TodoFooter",
    4. props:["todos","editAll","delAll"],
    5. computed: {
    6. alltodo() {
    7. return this.todos.length;
    8. },
    9. alldone(){
    10. //累加勾选的任务
    11. return this.todos.reduce((total,current)=>{
    12. return total + (current.done?1:0);
    13. },0);//0为初始值
    14. },
    15. //勾选状态
    16. checkall(){
    17. return this.alldone!=0 && this.alltodo==this.alldone;
    18. }
    19. },
    20. methods:{
    21. changeAll(e){
    22. // console.log(e.target.checked);
    23. this.editAll(e.target.checked);
    24. },
    25. pressDelAll(){
    26. this.delAll();
    27. }
    28. }
    29. }
    30. script>
    31. <style>
    32. style>

    Item.vue

    1. <script>
    2. export default{
    3. name:"Item",
    4. props:["item","delTodo","editDone"],
    5. methods:{
    6. pressDel(id){
    7. this.delTodo(id);//调用父组件回调函数 并传递id
    8. }
    9. },
    10. computed:{
    11. changedone:{
    12. //得到的值调用
    13. get(){
    14. return this.item.done;
    15. },
    16. //设置值的时候调用
    17. set(done){
    18. // console.log(this.item.id);
    19. this.editDone(this.item.id);
    20. }
    21. }
    22. }
    23. }
    24. script>
    25. <style>
    26. style>

     

  • 相关阅读:
    最好的蓝牙耳机是哪种?品牌蓝牙耳机排行榜
    MyBatis中只有一个参数时--如何判断null呢?
    解决userdel: user xxx is currently used by process 2461和kill命令无法杀死进程
    linux下安装openwrt(kvm虚拟机下安装)
    16.1 Socket 端口扫描技术
    element-ui upload图片上传组件使用
    jQuery 入门-----第一节:jQuery的概述
    Unity--自动版面(Grid Layout Group)
    Linux MySQL+docker MySQL 搭建主主(练手)
    runloop
  • 原文地址:https://blog.csdn.net/m0_59359854/article/details/126384671