Vue's idea of componentization modifies todolist

January 9, 2019 21:35:27 Comment Reading (1821)
This article was last updated on January 9, 2019, and has not been updated for more than a year. If the article content or image resources are invalid, please leave a message for feedback, and we will deal with it in a timely manner, thank you!

Vue组件化思想修改todolist

A page is composed of many parts, such as carousel and content area. If we write business logic on a page, it is not conducive to maintenance. Components are equivalent to one part of the page. A page can be cut into many parts, which can improve the maintainability of code and facilitate development.

Modify todolist based on the idea of componentization

Vue组件化思想修改todolist

As shown in the figure above, when entering any content, we will put it in the red box below. Each list item is a part of the page. Since it is a part, we can split it into a component.

source code

  1. < div   id = "app" >
  2.      < input   type = "text"   v-model = "inputValue" />
  3.      < button   v-on:click = "btnClick" > Submit </ button >
  4.      < ul >
  5.          < li   v-for = "item in List" > {{item}} </ li >
  6.      </ ul >
  7. </ div >
  8. ipt >
  9. var  app  =  new  Vue({
  10.     el:'#app',
  11.     data: {
  12.         List: [],
  13.         inputValue: ''
  14.     },
  15.     methods: {
  16.         btnClick: function () {
  17.             this. List.push(this.inputValue)
  18.              this.inputValue  =  ''
  19.         }
  20.     }
  21. })
  22. ript >

The current list item is through a loop LiLabel To show, now I want to LiLabel What should we do if the content of becomes a component as a whole?

  • Call a Component method The component is provided by vue for us to create Global Components The specific examples are as follows:
  1. Vue.component( "TodoList" ,{  //Define global components, where the name can be abbreviated as todo list
  2. props:['content'],   //Receive content from parent component
  3. template: "<li>{{content}}</li>"   //Component template (to be called with interpolation expression)
  • Use the todo list component. The specific example is as follows:
  1. < ul >
  2.      < todo-item   v-bind:content = "item"   v-for = "item in List" > </ todo-item >
  3. </ ul >

When cycling the List, assign each value of the List to the item, and then the item is passed to the todo item subcomponent through the variable content through the v-bind syntax.

Last Code

  1.     < div   id = "app" >
  2.         < input   type = "text"   v-model = "inputValue" />
  3.         < button   v-on:click = "btnClick" > Submit </ button >
  4.         < ul >
  5.             < todo-item   v-bind:content = "item"   v-for = "item in List" > </ todo-item >
  6.         </ ul >
  7.     </ div >
  8. <script >
  9. Vue.component("TodoItem",{
  10.        props:['content'],
  11.        template:" < li > {{content}} </ li > "
  12.    })
  13.    var  app  =  new  Vue({
  14.        el:'#app',
  15.        data: {
  16.            List: [],
  17.            inputValue: ''
  18.        },
  19.        methods: {
  20.            btnClick: function () {
  21.                this. List.push(this.inputValue)
  22.                 this.inputValue  =  ''
  23.            }
  24.        }
  25.    })
  26. </script >

 

End.


 

 weinxin
WeChat applet
Internet development, lifelong learners, welcome your attention!
 Willing

Comment

 unhappy  rainbow  look on  Diu Xiang  Good  funny  Flower core  Cry out  surprised  blink the eyes  Cool  Sad  Are you handsome?  gift  rose  anger  get angry  spray  Sleep?  Too happy  Little 999  ah
 sunlight  Spit out one's tongue  Grievance  smiling eyes  Star Moon  heart-broken  Eh  Insidious  doubt  that 's great  Snicker  Squint  shock  slightly  Yawn  Helpless to cry  Pick one's nose  Hum  expect  I don't care about you  love  candle