跳到主要内容

组件通信

一、prop 通信


1.1 prop 只读通信

<template>
<div>
<AComp :name="name"></AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
name: "为人进出的门紧锁着"
}
}
};
</script>

1.2 prop & $emit 组合通信

<template>
<div>
<AComp :name.sync="name" @setName="setName"></AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
name: "为人进出的门紧锁着"
}
},
methods:{
setName(name){
this.name = name;
}
}
};
</script>

1.3 prop.sync & $emit 组合通信

<template>
<div>
<AComp :name.sync="name"></AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
name: "为人进出的门紧锁着"
}
}
};
</script>

二、$refs 通信


<template>
<div>
<AComp ref="aComp"></AComp>
<button @click="setName">改变 AComp 的 name</button>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
methods: {
setName() {
this.$refs.aComp.setName('为狗爬出的洞敞开着');
},
},
};
</script>

二、$parent 通信


<template>
<div>
<AComp></AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
name: "为人进出的门紧锁着"
}
},
methods:{
setName(name){
this.name = name;
}
}
};
</script>

三、$children 通信


<template>
<div>
<AComp></AComp>
<button @click="setName">改变子组件 name</button>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data() {
return {};
},
methods: {
setName() {
this.$children[0].setName("为狗爬出的洞敞开着");
},
},
};
</script>

四、on & emit 组合通信


<template>
<div>
<AComp :name="name"></AComp>
</div>
</template>

<script>
import AComp from "./aComp.vue";

export default {
name: "App",
components: { AComp },
data(){
return {
name: "为人进出的门紧锁着"
}
},
mounted(){
this.$on('setName',this.setName)
},
methods:{
setName(name){
this.name = name;
}
}
};
</script>

五、provide & inject 组合通信


5.1 非响应式通信过程

provideinject 如果绑定的是基础数据, 那么是非响应的

<template>
<div>
<HelloWorld></HelloWorld>
<button @click="add">增加 Value</button>
</div>
</template>

<script>
import HelloWorld from "./HelloWorld.vue";

export default {
name: "App",
components: {
HelloWorld,
},
data() {
return {
value: 0,
};
},
provide() {
return {
value: this.value,
};
},
methods: {
add() {
this.value = this.value + 1;
console.log(this.value);
},
},
};
</script>

5.2 可响应式通信过程

provideinject 如果绑定的是响应式的对象,那么对象的 property 是可响应的

<template>
<div>
<HelloWorld></HelloWorld>
<button @click="addNum">App 组件 增加 State.Num</button>
<button @click="addListItem">App 组件 增加 State.List</button>
</div>
</template>

<script>
import HelloWorld from "./HelloWorld.vue";

export default {
name: "App",
components: {
HelloWorld,
},
data() {
return {
state: {
num: 0,
list: [],
},
};
},
provide() {
return {
state: this.state,
addNum: this.addNum,
addListItem: this.addListItem
};
},
methods: {
addNum() {
this.state.num = this.state.num + 1;
},
addListItem(){
const item = this.state.num;
this.state.list.push(item)
}
},
};
</script>