組件
組件 component:
組件(Component)是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素跟压,封裝可重用的代碼鸣哀。
組件可被反復(fù)調(diào)用或使用
全局組件
Vue.component('組件名’,{
template:` `
})
局部組件
new Vue({
el:"#app",
components:{
'組件名':{
template:` `
}
}
})
例:
div部分
<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
<ul>
<li></li>
</ul>
</div>
JS部分
<script src="../js/vue.js"></script>
<script>
//全局:
Vue.component('my-component',{
template:`
<ul>
<li>aaaa</li>
</ul>
`
})
new Vue({
el:'#app',
//局部
/* components:{
'my-component':{
template:``
}
}*/
})
</script>
1.組件中點(diǎn)擊按鈕彈出警示框
<div id='app'>
<!-- <p>{{msg}}</p>-->
<my-component></my-component>
</div>
<script src="../js/vue.js"></script>
<script>
Vue.component("my-component",{
template:`
<div>
<h1>{{msg}}</h1>
<button @click='alt'>按鈕</button>
</div>
`,
data:function(){
return{
msg:'dcgf'
}
},
methods:{
alt:function(){
alert(11111)
}
}
})
new Vue({
el:'#app',
})
</script>
效果如下:Image 1.png
2.組件的嵌套
組件之間的傳值
父?jìng)髯樱河脤傩詡?/p>
props:["屬性"]
子傳父:用事件傳
$emit:this.$emit('自定義事件','參數(shù)')
同級(jí)之間傳值/非父子之間傳值:
借助第三方量 var bus=new Vue() bus.$emit('send',msg=>{})
父?jìng)髯?/h1>
例:
<div id='app'>
<my-content></my-content>
</div>
<script src='../js/vue.js'></script>
<script>
Vue.component("my-content",{
template:`
<div>
<h2>我是my-content組件的標(biāo)題</h2>
<my-child v-bind:message='msg'></my-child>
</div>
`,
data:function(){
return{
msg:'dgddbghfghfnh'
}
}
})
Vue.component("my-child",{
props:['message'],
template:`
<div>
<h3>我是my-child組件中的標(biāo)題</h3>
<p>{{message}}</p>
</div>
`
})
new Vue({
el:'#app'
})
</script>
<div id='app'>
<my-content></my-content>
</div>
<script src='../js/vue.js'></script>
<script>
Vue.component("my-content",{
template:`
<div>
<h2>我是my-content組件的標(biāo)題</h2>
<my-child v-bind:message='msg'></my-child>
</div>
`,
data:function(){
return{
msg:'dgddbghfghfnh'
}
}
})
Vue.component("my-child",{
props:['message'],
template:`
<div>
<h3>我是my-child組件中的標(biāo)題</h3>
<p>{{message}}</p>
</div>
`
})
new Vue({
el:'#app'
})
</script>
效果如下:
Image 1.png
例:
<div id="app">
<my-father></my-father>
</div>
<script src='../js/vue.js'></script>
<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:['apple','pear','orange'],
title:'水果列表'
}
}
}),
//title
Vue.component("my-son",{
props:['tit'],
template:`
<h2>{{tit}}</h2>
`
}),
//arr
Vue.component('my-list',{
props:['fruit'],
template:`
<ul>
<li v-for="value in fruit">{{value}}</li>
</ul>
`
}),
new Vue({
el:"#app",
})
</script>
效果如下:
Image 2.png
3.水果列表
<div id="app">
<my-father></my-father>
</div>
<script src='../js/vue.js'></script>
<script>
Vue.component("my-father",{
template:`
<div>
<input type="text" v-model="fruit">
<button v-on:click="add">添加</button>
<my-child v-bind:fruList="list"></my-child>
</div>
`,
data:function(){
return{
list:["apple","pear","orange"],
fruit:""
}
},
methods:{
add:function(){
this.list.push(this.fruit)
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)
this.fruit=""
}
}
}),
new Vue({
el:"#app",
})
</script>
效果如下:
Image 3.png
4.shopping
<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'>編號(hào)</th>
<th class='text-center'>名稱</th>
<th class='text-center'>單價(jià)</th>
<th class='text-center'>數(shù)量</th>
<th class='text-center'>小計(jì)</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>總價(jià):¥{{sum}}</td>
</tr>
</tbody>
`,
data:function(){
return{
sum:0
}
},
methods:{
add:function(ind){
this.list[ind].count++ ;
//計(jì)算小計(jì)
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--
}
//計(jì)算小計(jì)
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>
效果如下:
Image 4.png
子傳父
例:
<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:'JSS是共產(chǎn)主義接班人'
}
},
methods:{
sendFather:function(){
// this.$emit('自定義事件',參數(shù))
this.$emit('send',this.msg)
}
}
}),
new Vue({
el:"#app",
})
</script>
效果如下:
Image 5.png
5.子傳父
<div id="app">
<my-father></my-father>
</div>
<script src="../js/vue.js"></script>
<script>
Vue.component("my-father",{
template:`
<div>
<h1>這個(gè)是父組件</h1>
<span>請(qǐng)?jiān)诟附M件中添加數(shù)據(jù)</span>
<span>{{mess}}</span>
<my-child @send='revMsg'></my-child>
</div>
`,
data:function(){
return{
mess:""
}
},
methods:{
revMsg:function(txt){
this.mess=txt
}
}
}),
Vue.component("my-child",{
template:`
<div>
<h1>這個(gè)是子組件</h1>
<input type="text" v-model="msg">
<button @click='sendFather'>給父組件</button>
</div>
`,
data:function(){
return{
msg:"",
text:""
}
},
methods:{
sendFather:function(){
this.$emit('send',this.msg)
this.msg=this.text
}
}
}),
new Vue({
el:"#app",
})
</script>
效果如下:
Image 1.png