最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue組件間通信解析

 更新時(shí)間:2017年03月01日 14:50:31   作者:vichily  
這篇文章主要為大家詳細(xì)介紹了vue組件間通信解析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

組件間通信(父子,兄弟)

相關(guān)鏈接\組件通信:點(diǎn)擊查看

學(xué)習(xí)鏈接:Vue.js——60分鐘快速入門(mén)點(diǎn)擊查看

分分鐘玩轉(zhuǎn)Vue.js組件點(diǎn)擊查看

父組件傳子組件

父?jìng)髯臃椒ǎㄒ唬?/strong> 屬性傳遞 props

//子組件
<template> 
 <ul>
 <li v-for="item in dataList">{{item}}</li>
 </ul> 
</template>

<script>
 export default { 
 props : { dataList : [] }
 }
</script>

//父組件
<template>
 <component-child v-bind:data-list="dataList"> </component-child> 
 <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input>
</template>

<script>

import ComponentChild from './child.vue'
export default { 
 data () { 
 return { 
 dataInput: "", 
 dataList : [ 'hello world!','welcome to use vue.js' ] 
 } 
 }, 
 components : { ComponentChild }, 
 methods : { 
 addDataItem () { 
 let self = this 
 if( !(self.dataInput && self.dataInput.length > 0) ) { return } 
 self.dataList.push( self.dataInput ) 
 self.dataInput = "" 
 } 
 }
}
</script>

父?jìng)髯臃椒ǎǘ?/strong> 廣播事件傳遞 vm.$broadcast

//子組件
<template> 
 <ul> 
 <li v-for="item in dataList">{{item}}</li> 
 </ul> 
</template>

<script>
export default { 
 data () { 
 return { 
 dataList : [ 'hello world!', 'welcome to use vue.js' ] 
 } 
 }, 
 events : { 
 addChildDataEvent : function ( dataInput ) { 
 this.dataList.push( dataInput ) 
 } 
 }
}
</script>

//父組件
<template> 
 <component-child></component-child> 
 <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input>
</template>

<script>
 import ComponentChild from './child.vue'
 export default { 
 data () { 
 return { dataInput: "" } 
 }, 
 components : { ComponentChild }, 
 methods : { 
 addDataItem () { 
 let self = this 
 if( !(self.dataInput && self.dataInput.length > 0) ) { return } 
 //廣播到子組件 
 self.$broadcast( 'addChildDataEvent', self.dataInput ) 
 self.dataInput = "" } 
 }
 }
</script>

子組件傳父組件

子傳父方法 派遣事件傳遞 vm.$dispatch

//子組件
<template> 
 <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input>
</template>

<script>
 export default { 
 data () { 
 return { 
 dataInput: "" 
 } 
 }, 
 methods : { 
 addDataItem () { 
 let self = this
 if( !(self.dataInput && self.dataInput.length > 0) ) { return }
 //派遣事件到父組件 
 self.$dispatch( 'addFatherDataEvent', self.dataInput )
 self.dataInput = "" 
 } 
 }
 }
</script>

//父組件
<template> 
 <ul> 
 <li v-for="item in dataList">{{item}}</li> 
 </ul> 
 <component-child></component-child>
</template>

<script>
import ComponentChild from './child.vue'
export default { 
 data () { 
 return { 
 dataList : [ 'hello world!', 'welcome to use vue.js' ] 
 } 
 },
 components : { ComponentChild }, 
 events : { 
 addFatherDataEvent : function ( dataInput ) { 
 this.dataList.push( dataInput ) 
 } 
 }
}
</script>

兄弟組件互傳

父組件代理傳遞 子(vm.dispatch )父 ( vm.broadcast )子 事件方法傳遞

<template> 
 <ul> 
 <li v-for="item in dataList">{{item}}</li> 
 </ul> 
</template>

<script> 
export default { 
 data () { 
 return { 
 dataList : [ 'hello world!', 'welcome to use vue.js' ] 
 } 
 },
 events : { 
 addChildDataEvent : function ( dataInput ) { 
 this.dataList.push( dataInput ) 
 } 
 }
}
</script>

<template>
 <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input>
</template>

<script>
export default { 
 data () { 
 return { dataInput: "" } 
 }, 
 methods : { 
 addDataItem () { 
 let self = this 
 if( !(self.dataInput && self.dataInput.length > 0) ) { return } //派遣事件到父組件 
 self.$dispatch( 'agentDataEvent', self.dataInput ) 
 self.dataInput = "" 
 }
 }
}
</script>

<template> 
<component-child1></component-child1>
<component-child2></component-child2>
</template>

<script>
import ComponentChild1 from './child1.vue'
import ComponentChild2 from './child2.vue'

export default { 
 components : { ComponentChild1, ComponentChild2 }, 
 events : { 
 agentDataEvent : function( dataInput ) { 
 this.$broadcast('addChildDataEvent', dataInput) 
 } 
 }
}
</script>

實(shí)例間通信

把實(shí)例當(dāng)做參數(shù)傳入另一個(gè)實(shí)例

<template>
 <div> 
 <p>實(shí)例間通信</p> 
 <ul>
 <li v-for="item in dataList">{{item}}</li>
 </ul> 
 </div>
</template>
<script> 
export default { 
 data () { 
 return { 
 dataList : [ 'hello world!', 'welcome to use vue.js' ] 
 } 
 }, 
 events : { 
 addDataEvent : function ( dataInput ) { 
 this.dataList.push( dataInput ) 
 } 
 }
}
</script>
<template>
<input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input>
</template>

<script>
export default { 
 data () { 
 return { 
 dataInput: "", 
 otherApp : {} 
 } 
 }, 
 methods : { 
 addDataItem ( ) { 
 let self = this 
 if( !(self.dataInput && self.dataInput.length > 0) ) { return } //觸發(fā)其他實(shí)例事件 
 self.otherApp.$emit( 'addDataEvent', self.dataInput ) 
 self.dataInput = "" 
 }, 
 setOtherApp ( app ) { 
 this.otherApp = app 
 }
 }
 
}
</script>

import Vue from "vue"
import App1 from "./communication5/app1.vue"
import App2 from "./communication5/app2.vue"

let AppVM1 = new Vue( App1 ).$mount('#app')
let AppVM2 = new Vue( App2 ).$mount('#app2')

AppVM2.setOtherApp( AppVM1 )

本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。

關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

天津市| 剑河县| 德钦县| 抚州市| 舒城县| 寿宁县| 双柏县| 淮南市| 梓潼县| 日喀则市| 二手房| 比如县| 金阳县| 龙海市| 清远市| 泗阳县| 高雄县| 阳谷县| 金阳县| 上饶县| 建湖县| 江孜县| 山阳县| 五常市| 怀来县| 柏乡县| 霍林郭勒市| 锦屏县| 栖霞市| 四子王旗| 南召县| 民县| 玉门市| 乃东县| 密山市| 潼南县| 舞阳县| 新乡县| 浮山县| 平远县| 铜鼓县|