Vue——组件化todolist

简介: 组件化todolist

index部分

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" type="text/css" href="css/todolist.css" />
</head>
<body>
  <div id="app">
  <header>
    <form @submit.prevent='submit($event)'>
    <label for="inpTitle">ToDoList</label>
    <input type="text" id="inpTitle" placeholder="添加ToDo" required="required" />
    </form>
  </header>
  <section>
    <ullist title="正在进行" :local="local" :num="todonum" :flag="todoflag" @dellist="delitem"
    @editdatalist="editdataitem(arguments)" @changedatalist="changedataitem"></ullist>
    <ullist title="已完成" :local="local" :num="donenum" :flag="doneflag" @dellist="delitem"
    @editdatalist="editdataitem(arguments)" @changedatalist="changedataitem"></ullist>
  </section>
  <footer>
    <p>Copyright &copy; 2014 todolist.cn <a @click="clearLocal()">clear</a></p>
  </footer>
  </div>
  <template id="ullist">
  <div>
    <slot>
    <h2>{{title}}<span>{{num}}</span></h2>
    <ul>
      <li v-for="(item,index) in local" :key="index" v-if="item.todo===flag">
      <input type="checkbox" @change="changedata(index)" :checked="flag" />
      <input type="text" v-model="item.title" @blur="editdata($event,index)" />
      <a @click="del(index)">-</a>
      </li>
    </ul>
    </slot>
  </div>
  </template>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
  <script>
  const ullist = {
    template: '#ullist',
    props: {
    title: {
      type: String
    },
    local: {
      type: Array
    },
    flag: {
      type: Boolean,
      default: false
    },
    num: {
      type: Number,
      default: 0
    }
    },
    methods: {
    del(index) {
      this.$emit('dellist', index)
    },
    editdata(e, index) {
      this.$emit('editdatalist', e, index)
    },
    changedata(index) {
      this.$emit('changedatalist', index)
    }
    }
  }
  </script>
  <script src="js/todolist.js"></script>
</body>
</html>

css部分:

*{
  margin: 0;
  padding: 0;
}
a{
  text-decoration: none;
  cursor: pointer;
}
li{
  list-style: none;
}
body{
  background-color: #CDCDCD;
}
header{
  width: 100%;
  height: 50px;
  background-color: #323232;
}
header form{
  width: 620px;
  height: 50px;
  margin: 0 auto;
}
form label{
  color: #DDDDDD;
  font-size: 24px;
  line-height: 50px;
  cursor: pointer;
}
form input{
  width: 364px;
  height: 26px;
  outline: 0;
  padding-left: 10px;
  box-sizing: border-box;
  border-radius: 5px;
  background-color: #FFFFFF;
  float: right;
  margin-top: 12px;
  border: none;
  box-shadow: 0 1px 0 rgb(255 255 255 / 24%), 0 1px 6px rgb(0 0 0 / 45%) inset;
}
section{
  width: 620px;
  padding: 0px 10px;
  box-sizing: border-box;
  margin: 0 auto;
}
section h2{
  margin: 20px 0px;
}
section h2 span{
  display: inline-block;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #666666;
  font-size: 14px;
  background-color: #E6E6FA;
  padding: 0px 5px;
  border-radius: 20px;
  float: right;
  margin-top: 6px;
}
section ul li{
  height: 32px;
  background-color: #FFFFFF;
  border-left: 5px solid #629A9C;
  line-height: 32px;
  margin-bottom: 10px;
  border-radius: 3px;
  box-shadow: 0 1px 2px rgb(0 0 0 / 7%);
  padding: 0px 45px;
  position: relative;
}
section ul li p{
  margin: 0 auto;
  height: 32px;
}
section ul li>input:nth-of-type(1){
  width: 22px;
  height: 22px;
  position: absolute;
  top: 50%;
  left: 12px;
  margin-top: -11px;
}
section ul li>input:nth-of-type(2){
  width: 100%;
  height: 90%;
  box-sizing: border-box;
  border: none;
  font-size: 16px;
}
section ul li>input:nth-of-type(2):focus{
  outline: 1px solid #333333;
  padding-left: 5px;
}
section ul li a{
  display: block;
  width: 26px;
  height: 24px;
  line-height: 12px;
  text-align: center;
  box-sizing: border-box;
  border-radius: 14px;
  border: 6px double #FFF;
  background: #CCC;
  color: #FFFFFF;
  font-weight: 700;
  position: absolute;
  top: 50%;
  right: 12px;
  margin-top: -12px;
}
footer{
  color: #666666;
  font-size: 14px;
  text-align: center;
}
footer a{
  color: #999999;
}

JS部分

const app = new Vue({
  el: '#app',
  data() {
  return {
    local: [],
    todoflag: false,
    doneflag: true,
    todonum: 0,
    donenum: 0
  }
  },
  components: {
  ullist
  },
  methods: {
  submit(e) {
    console.log(e.target[0].value);
    let newtodo = {
    "title": e.target[0].value,
    "todo": false
    }
    e.target[0].value = ''
    //点击回车增加数据
    this.local.unshift(newtodo)
    //存储数据
    this.savedata()
    this.count()
  },
  //存储数据
  savedata() {
    window.localStorage.setItem('todo', JSON.stringify(this.local))
  },
  //删除数据
  delitem(index) {
    //删除点击的某一项
    this.local.splice(index, 1)
    //存储数据
    this.savedata()
    this.count()
  },
  //编辑数据
  editdataitem(e) {
    console.log(e[0]); //代表点击某一项失焦事件
    console.log(e[1]); //索引
    var index = e[1]
    this.local[index].title = e[0].target.value
    //存储数据
    this.savedata()
  },
  //切换数据
  changedataitem(index) {
    console.log(index);
    this.local[index].todo ? this.local[index].todo = false : this.local[index].todo = true,
    //存储数据
    this.savedata()
    this.count()
  },
  //数据个数
  count() {
    this.donenum = 0;
    this.todonum = 0;
    this.local.forEach((v, index) => {
    v.todo ? this.donenum++ : this.todonum++
    })
  }
  },
  created() {
  let data = window.localStorage.getItem('todo')
  if (data != null) {
    console.log(JSON.parse(data));
    this.local = JSON.parse(data)
    this.count()
  } else {
    this.local = []
  }
  }
})
相关文章
|
3月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
311 2
|
2月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
291 137
|
6月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
793 0
|
6月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
5月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
419 1
|
5月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
262 0
|
6月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
473 17
|
6月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
278 1
|
6月前
|
存储 JavaScript 前端开发
如何高效实现 vue 文件批量下载及相关操作技巧
在Vue项目中,实现文件批量下载是常见需求。例如文档管理系统或图片库应用中,用户可能需要一次性下载多个文件。本文介绍了三种技术方案:1) 使用`file-saver`和`jszip`插件在前端打包文件为ZIP并下载;2) 借助后端接口完成文件压缩与传输;3) 使用`StreamSaver`解决大文件下载问题。同时,通过在线教育平台的实例详细说明了前后端的具体实现步骤,帮助开发者根据项目需求选择合适方案。
521 0
|
6月前
|
JavaScript 前端开发 UED
Vue 项目中如何自定义实用的进度条组件
本文介绍了如何使用Vue.js创建一个灵活多样的自定义进度条组件。该组件可接受进度段数据数组作为输入,动态渲染进度段,支持动画效果和内容展示。当进度超出总长时,超出部分将以红色填充。文章详细描述了组件的设计目标、实现步骤(包括props定义、宽度计算、模板渲染、动画处理及超出部分的显示),并提供了使用示例。通过此组件,开发者可根据项目需求灵活展示进度情况,优化用户体验。资源地址:[https://pan.quark.cn/s/35324205c62b](https://pan.quark.cn/s/35324205c62b)。
257 0