props
和$emit
父组件向子组件传递数据是经过prop
传递的,子组件传递数据给父组件是经过$emit
触发事件来作到的.sync
和 v-model
的使用(父子组件数据同步)$parent
,$children
多层级传递数据,智能组件封装$attrs
(属性的集合)和$listeners
(方法的集合)。Vue 2.4 开始提供了$attrs
和$listeners
来解决A->B->C问题,组件间向下传递,能够不用 props
注册。v-bind
属性传递,v-on
方法传递provider
来提供变量,而后在子组件中经过inject
来注入变量。$refs
获取实例envetBus
平级组件数据传递 这种状况下可使用中央事件总线的方式父组件vue
<template>
<div>
父组件:{{money}}
<son1 :money='money' @addMoney = 'addMoney'></son1>
</div>
</template>
<script>
import son1 from './son1'
export default {
components:{
son1
},
data(){
return {
money:100
}
},
methods:{
addMoney(val){
this.money = val
}
}
}
</script>
复制代码
子组件 son1.vuevue-router
<template>
<div>
儿子1得到{{money}}
<button @click="raiseMoney">多给点嘛</button>
</div>
</template>
<script>
export default {
props:{
money:{
type:Number,
default:1
}
},
methods:{
raiseMoney(){
this.$emit('addMoney',200)
}
}
}
</script>
复制代码
.syncbash
update:myPropName
的模式触发事件取而代之父组件 <son1 :money.sync='money'></son1>
子组件 this.$emit('update:money',200)
父组件 <son1 :money="money" @update:money="(val)=>this.money=val"></son1>
子组件this.$emit('update:money',200)
v-modelapp
<son1 :value="money" @input="(val)=>this.money=val></son1>
<son1 v-model="money"></son1>
value
,具备局限性,而.sync
则能够随意起名字$parent
$children
<button @click="$parent.$emit('addMoney',400)">更改父组件</button>
$parent
下去,这样会很麻烦,不如直接封装一个$dispatch
$dispatch
只会通知本身对父亲, 而eventBus
是全局通知,通知全部的父亲或者儿子dispatch
向上通知dom
Vue.prototype.$dispatch = function (eventName,value){
let parent = this.$parent;
while(parent){
parent.$emit(eventName,value);
parent = parent.$parent
}
}
使用时只须要 this.$dispatch('addMoney',400)
//666 亲测无误
复制代码
向下传递ide
$broadcast
Vue.prototype.$broadcast = function (eventName,value){
//获取当前组件下的因此孩子
const broadcast = (children) => {
children.forEach(child => {
child.$emit(eventName,value);
if(child.$children){
broadcast(child.$children)
}
})
}
broadcast(this.$children)
}
使用时只须要 在父级 this.$broadcast('say',1111) 只要子组件中有绑say的都给我说1111
在son1.vue中
<Grandson :money = money @say='say1'></Grandson>
methods:{
say1(val){
console.log("我很帅"+val)
}
}
<!--亲测控制台打印 我很帅1111-->
复制代码
$attrs
属性的集合$attrs
表明上级传过来的全部属性,当父组件给子组件须要穿过来不少值时,用props一个一个接收岂不是很累,使用$attrs
就能够一次性把父级传过来当全部值接收到,完美~$attrs
接收完数据,打开控制台,你会发现以下状况,属性所有挂载在了dom上,咱们并不想这样
$attrs
的子组件上,添加inheritAttrs:false
,代码以下<!--父组件-->
<son2 name="奔跑" address="北京"></son2>
<!--子组件-->
template>
<div>
<!-- $attrs表明上级传过来的全部属性 -->
儿子2: {{$attrs}}
</div>
</template>
<script>
export default {
inheritAttrs:false
}
</script>
<!--页面显示:儿子2: { "name": "奔跑", "address": "北京" }-->
复制代码
<grandson2 v-bind="$attrs"></grandson2>
$attrs
便可得到全部的数据$listeners
方法的集合$listeners
来调用这些方法v-on="$listeners"
来传递给孙子组件这些方法,孙子组件使用时,一样使用$listeners
<!--父组件-->
<son2 name="奔跑" address="北京" @look="console.log('look')"></son2>
<!--子组件-->
<grandson2 v-bind="$attrs" v-on="$listeners"></grandson2>
<!--孙子组件-->
<template>
<div>
孙子2:{{$attrs}}
<button @click="$listeners.look()">看</button>
</div>
</template>
<!--在孙子组件上点击按钮'看',则会在控制台打印'look'-->
复制代码
provide
提供变量,inject
注册变量this.$router.go(0)
;location.reload()
; window.open
一下【祖先组件(provide )向其全部子孙后代(inject )注入一个依赖】ui
$refs
获取实例ref
被用来给元素或者子组件注册引用信息。引用信息将会注册在父组件的$refs
对象上。$refs
;$refs
相对document.getElementById
的方法,会减小获取dom节点的消耗。$refs
只在组件渲染完成后才填充,而且它是非响应式当。它仅仅是一个直接操做子组件的应急方案---应当避免在模版或者计算属性中使用$refs
<!--父级-->
<son2 name="奔跑" address="北京" ref="son2"></son2>
mounted(){
this.$refs.son2.sleep()
},
<!--son2子组件-->
methods:{
sleep(){
console.log('我想睡觉')
}
}
复制代码
eventBus
定义到了全局上,经过$on(eventName,function)
注册事件,经过$emit(eventName,参数)
获取使用事件<!--在mian.js上注册bus-->
Vue.prototype.$bus = new Vue();
<!--在恰当的页面 注册事件-->
this.$bus.$on('有人找我了',function(val){
console.log('呵呵'+val)
})
<!--在恰当的页面 使用事件-->
this.$nextTick(()=>{
this.$bus.$emit('有人找我了','xxx')
})
<!--控制台打印-->
呵呵xxx
复制代码