to-do-list

叁歲伎倆 2022-05-31 23:42 331阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <script type="text/javascript" src="js/vue.js"></script>
  6. </head>
  7. <body>
  8. <div id="example">
  9. <to-do-box></to-do-box>
  10. </div>
  11. <script type="text/javascript">
  12. //按组件树的方式code 组件:代码复用
  13. var bus=new Vue({});
  14. Vue.component('to-do-item',{
  15. props:['sendMsg','myIndex'],
  16. methods:{
  17. deleteItem(){
  18. this.$emit('deleteEvent',this.myIndex);
  19. //this.parents.msgList.splice(this.myIndex,1); 另一种方法
  20. }
  21. },
  22. template:`
  23. <li>
  24. <button @click="deleteItem">delete</button>
  25. <span>{
  26. {sendMsg}}</span>
  27. </li>
  28. `
  29. });
  30. Vue.component('to-do-list',{
  31. data:function(){
  32. return {
  33. msgList:[]
  34. }
  35. },
  36. created:function(){
  37. //var that=this;
  38. bus.$on('addEvent',(msg)=>{
  39. console.log("接收到数据为:"+msg);
  40. this.msgList.push(msg);
  41. })
  42. },
  43. methods:{
  44. rcvDeleteMsg(index){
  45. this.msgList.splice(index,1);
  46. }
  47. },
  48. template:`
  49. <ul>
  50. <to-do-item v-for="(tmp,index) in msgList" :sendMsg="tmp" :myIndex="index" :key="index" @deleteEvent="rcvDeleteMsg"></to-do-item>
  51. </ul>
  52. `
  53. });
  54. Vue.component('to-do-input',{
  55. data:function(){
  56. return {
  57. userInput:''
  58. }
  59. },
  60. methods:{
  61. handleClick(){
  62. bus.$emit('addEvent',this.userInput);
  63. }
  64. },
  65. template:`
  66. <div>
  67. <h2>待做事项列表</h2>
  68. <input type="text" placeholder="请输入" v-model='userInput'/>
  69. <button @click="handleClick">add</button>
  70. </div>
  71. `
  72. });
  73. Vue.component('to-do-box',{
  74. template:`
  75. <div>
  76. <to-do-input></to-do-input>
  77. <to-do-list></to-do-list>
  78. </div>
  79. `
  80. })
  81. new Vue({
  82. el:'#example',
  83. data:{
  84. msg:'VueJS is Awesome'
  85. }
  86. });
  87. </script>
  88. </body>
  89. </html>

发表评论

表情:
评论列表 (有 0 条评论,331人围观)

还没有评论,来说两句吧...

相关阅读