1琳袄、component全局組件
Vue.component('todo-item',{
template:'<li>item</li>'
})
2君旦、局部組件
A澎办、定義局部組件
var Todo-item={
template:'<li>item</li>'
})
B、局部組件需要到Vue中聲明引用
component:{
'todo-item'=Todo-item
}
3金砍、外部向組件傳參
A局蚀、外部定義:content="item",傳參
B、組件中定義props:["content"]接收參數(shù)
C恕稠、在組件模板中參數(shù)化引用template:'<li>{{content}}</li>'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue學(xué)習(xí)</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<div v-if="show">hello</div>
<input v-model="inputValue"/>
<button @click="handleClick">click</button>
<ul>
<todo-item v-for="(item, index) of list"
:key="index"
:content="item"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todo-item',{
props:['content'],
template:'<li>{{content}}</li>'
})
new Vue({
el:"#root",
data:{
show:true,
list:[]
},
methods:{
handleClick:function () {
this.list.push(this.inputValue);
this.inputValue = "";
}
},
})
</script>
</body>
</html>