組件
組件(component):是vue最強(qiáng)大的功能之一 組件化開發(fā)
????????????????????????????????????組件可以拓展?HTML?元素,封裝可重用代碼
??????????????
??????????????
組件分為兩種:全局組件逗物、局部組件
全局組件代碼:
例子:
<div id="app">
<aa></aa>
</div>
<script>
Vue.component('aa',{
template:`
<ul>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
`
})
new Vue({
el:'#app'
})
</script>
局部組件代碼:
例子:
<div id="app">
<aa></aa>
</div>
<script>
new Vue({
el:'#app',
components:{
'aa':{
template:`
<ul>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
`
}
}
})
</script>
例子:
<div id="app">
<aa></aa>
</div>
<script>
Vue.component("aa",{
template:`
<div>
<h1>{{msg}}</h1>
<button @click='alt'>按鈕</button>
</div>
`,
data:function(){
return{
msg:'dcgf'
}
},
methods:{
alt:function(){
alert(11111)
}
}
})
new Vue({
el:'#app'
})
組件之間的傳值:
????????????父傳子:用屬性名?????props:['屬性']
????????????子傳父:用事件名??????$emit
????????????同級之間傳值:第三方量
全局組件代碼:
例子1:
<div id="app">
<my-component></my-component>
</div>
<script>
Vue.component("my-component",{
template:`
<div>
<h2>我是my-component組件的標(biāo)題</h2>
<my-child v-bind:zxc='zxx'></my-child>
</div>
`,
data:function(){
return{
zxx:'fsadgdrvgrf'
}
}
})
Vue.component("my-child",{
props:['zxc'],
template:`
<div>
<h3>我是my-child組件中的標(biāo)題</h3>
<p>{{zxc}}</p>
</div>
`
})
new Vue({
el:'#app'
})
</script>
例子1:
<div id="app">
<my-father></my-father>
</div>
<script>
Vue.component("my-father",{
template:`
<div>
<my-son v-bind:tit='title'></my-son>
<my-list v-bind:fruit='arr'></my-list>
</div>
`,
data:function(){
return{
arr:['蘋果','梨子','橘子'],
title:'水果列表'
}
}
})
Vue.component("my-son",{
props:['tit'],
template:`
<h2>{{tit}}</h2>
`
})
Vue.component("my-list",{
props:['fruit'],
template:`
<ul>
<li v-for='value in fruit'>{{value}}</li>
</ul>
`
})
new Vue({
el:'#app'
})
</script>
例子2:
<div id="app">
<my-father></my-father>
</div>
<script>
Vue.component('my-father',{
template:`
<div>
<input type='text' v-model='fruit'> <button @click='add'>添加</button>
<my-child v-bind:fruList='list'></my-child>
</div>
`,
data:function(){
return{
list:['蘋果','梨子','香蕉'],
fruit:''
}
},
methods:{
add:function(){
this.list.push(this.fruit)
}
}
})
Vue.component('my-child',{
props:['fruList'],
template:`
<ul>
<li v-for="(value,index) in fruList">
{{value}}
<button @click='delt(index)'>刪除</button>
</li>
</ul>
`,
methods:{
delt:function(ind){
this.fruList.splice(ind,1)
}
}
})
new Vue({
el:'#app'
})
</script>
例子3:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<div id='app'>
<my-father></my-father>
</div>
<script src="js/vue.js"></script>
<script>
Vue.component('my-father',{
template:`
<div class='container'>
<table class='table table-bordered text-center'>
<thead>
<tr>
<th class='text-center'>編號</th>
<th class='text-center'>名稱</th>
<th class='text-center'>單價</th>
<th class='text-center'>數(shù)量</th>
<th class='text-center'>小計</th>
</tr>
</thead>
<my-child v-bind:list='fruList'></my-child>
</table>
</div>
`,
data:function(){
return{
fruList:[
{pname:'apple',price:3,count:3,sub:9},
{pname:'pear',price:4,count:4,sub:16},
{pname:'orange',price:5,count:5,sub:25}
]
}
}
})
Vue.component('my-child',{
props:['list'],
template:`
<tbody>
<tr v-for="(value,index) in list">
<td>{{index+1}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>
<button @click='add(index)'>+</button>
<span>{{value.count}}</span>
<button @click='redu(index)'>-</button>
</td>
<td>{{value.sub}}</td>
</tr>
<tr>
<td colspan=5>總價:{{sum}}</td>
</tr>
</tbody>
`,
data:function(){
return{
sum:50
}
},
methods:{
add:function(ind){
this.list[ind].count++ ;
//計算小計
this.list[ind].sub=this.list[ind].count*this.list[ind].price;
this.countSum();
},
redu:function(ind){
if(this.list[ind].count>1){
this.list[ind].count--
}
//計算小計
this.list[ind].sub=this.list[ind].count*this.list[ind].price;
this.countSum();
},
countSum:function(){
for(var i=0,total=0;i<this.list.length;i++){
total+=this.list[i].sub;
}
this.sum=total;
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>
全局組件代碼:
例子1:
<div id='app'>
<my-father></my-father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component('my-father',{
template:`
<div>
<h1>{{mess}}</h1>
<my-child @send='revMsg'></my-child>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
revMsg:function(txt){
this.mess=txt
}
}
})
Vue.component('my-child',{
template:`
<button @click='sendFather'>給父組件</button>
`,
data:function(){
return{
msg:'張世遠(yuǎn)是共產(chǎn)主義接班人'
}
},
methods:{
sendFather:function(){
// this.$emit('自定義事件',參數(shù))
this.$emit('send',this.msg)
}
}
})
new Vue({
el:"#app"
})
</script>
例子2:
<div id='app'>
<father></father>
</div>
<script src='js/vue.js'></script>
<script>
Vue.component('father',{
template:`
<div>
<h1>{{mess}}</h1>
<son @send='rcvMsg'></son>
</div>
`,
data:function(){
return{
mess:''
}
},
methods:{
rcvMsg:function(txt){
this.mess=txt;
}
}
})
Vue.component('son',{
template:`
<button @click='sendToFather'>我是子組件季俩,我要向父組件傳值</button>
`,
data:function(){
return{
msg:'子組件向父組件傳值'
}
},
methods:{
sendToFather:function(){
this.$emit('send',this.msg )
}
}
})
new Vue({
el:"#app",
})
</script>
例子:
<div id="app">
<chat></chat>
</div>
<script>
Vue.component('chat',{
template:`
<div>
<ul>
<li v-for='value in arr'>{{value}}</li>
</ul>
<user @send='rcvMsg' userName='jack'></user>
<user @send='rcvMsg' userName='rose'></user>
</div>
`,
data:function(){
return{
arr:[]
}
},
methods:{
rcvMsg:function(txt){
this.arr.push(txt)
}
}
})
Vue.component('user',{
props:['userName'],
template:`
<div>
<ul>
<label>{{userName}}</label>
<input type="text" v-model='inputVal'>
<button @click='sendMsg'>發(fā)送</button>
</ul>
</div>
`,
data:function(){
return{
inputVal:''
}
},
methods:{
sendMsg:function(){
this.$emit('send',this.userName+':'+this.inputVal)
}
}
})
new Vue({
el:'#app'
})
</script>
全局組件代碼:
例子:
<div id="app">
<child></child>
<son></son>
</div>
<script>
var bus=new Vue();
Vue.component('child',{
template:`
<div>
<h1>我是child組件</h1>
<button @click='sendMsg'>發(fā)送數(shù)據(jù)給son</button>
</div>
`,
data:function(){
return{
msg:'hello vue'
}
},
methods:{
sendMsg:function(){
bus.$emit('send',this.msg)
}
}
})
Vue.component('son',{
template:`
<div>
<h1>我是son組件</h1>
<a href=''>{{mess}}</a>
</div>
`,
data:function(){
return{
mess:''
}
},
mounted:function(){
bus.$on('send',msg=>{
console.log(this);
this.mess=msg
})
}
})
new Vue({
el:'#app'
})
</script>