Modify todolist based on the idea of componentization
source code
-
< div id = "app" > -
< input type = "text" v-model = "inputValue" /> -
< button v-on:click = "btnClick" > Submit </ button > -
< ul > -
< li v-for = "item in List" > {{item}} </ li > -
</ ul > -
</ div > -
ipt > -
var app = new Vue({ -
el:'#app', -
data: { -
List: [], -
inputValue: '' -
}, -
methods: { -
btnClick: function () { -
this. List.push(this.inputValue) -
this.inputValue = '' -
} -
} -
}) -
ript >
-
Call a Component method The component is provided by vue for us to create Global Components The specific examples are as follows:
-
Vue.component( "TodoList" ,{ //Define global components, where the name can be abbreviated as todo list -
props:['content'], //Receive content from parent component -
template: "<li>{{content}}</li>" //Component template (to be called with interpolation expression)
-
Use the todo list component. The specific example is as follows:
-
< ul > -
< todo-item v-bind:content = "item" v-for = "item in List" > </ todo-item > -
</ ul >
Last Code
-
< div id = "app" > -
< input type = "text" v-model = "inputValue" /> -
< button v-on:click = "btnClick" > Submit </ button > -
< ul > -
< todo-item v-bind:content = "item" v-for = "item in List" > </ todo-item > -
</ ul > -
</ div > -
<script > -
Vue.component("TodoItem",{ -
props:['content'], -
template:" < li > {{content}} </ li > " -
}) -
var app = new Vue({ -
el:'#app', -
data: { -
List: [], -
inputValue: '' -
}, -
methods: { -
btnClick: function () { -
this. List.push(this.inputValue) -
this.inputValue = '' -
} -
} -
}) -
</script >