vue3组件通信的⽅式总结及实例⽤法vue3组件通信⽅式为以下⼏种
props
$emit
$expose / ref
$attrs
v-model
provide / inject
Vuex
mitt
props
<child :msg2="msg2" />vue中reactive
<script setup>
const props = defineProps({
// 写法⼀
msg2:String
// 写法⼆
msg2:{
type:String,
default:''
}
})
console.log(props) // {msg2:'这是传级⼦组件的信息2'}
</script>
$emit
//Child.vue
<template>
// 写法⼀
<div @click="emit('myclick')">按钮</div>
// 写法⼆
<div @click="handleClick">按钮</div>
</template>
<script setup>
// ⽅法⼀
const emit = defineEmits(['myClick'],['myClick2'])
// ⽅法⼆
const handleClick = () => {
emit('myClick','这是发送给⽗组件的信息');
}
// ⽅法⼆不适⽤于vue3.2,使⽤的useContext()已经舍弃
import { useContext } from 'vue'
const { emit } = useContext()
const handleClick = () => {
emit('myClick','这是发送给⽗组件的信息'
}
</script>
// Parent.vue响应
<template>
<child @myClick="onMyClick"></child>
</template>
<script setup>
import child from "./child.vue"
import onMychilk = (msg) => {
console.log(msg) // ⽗组件收到的信息
}
</script>
expose / ref
⽗组件获取⼦组件的属性或者调⽤⼦组件⽅法
<script setup>
// ⽅法⼀:useContext() vue3.2 之后已经舍弃
import { useContext } from 'vue'
const ctx = useContext()
// 对外暴露属性⽅法等都可以
childName: '这是⼦组建的属性',
someMethod(){
console.log('这是⼦组件的⽅法')
}
})
</script>
// Parent.vue 注意 ref="comp"
<template>
<child ref="comp"></child>
<button @click="handleClick">按钮</button>
</template>
<script>
import child from './child.vue'
import { ref } from 'vue'
const comp = ref(null)
const handleClick = () => {
console.log(comp.value.childName)
comp.value.someMethod() // 调⽤⼦组件对外暴露的⽅法
}
</script>
attts
attrs:包含⽗作⽤域除class和style除外的⾮props属性集合// ⽗组件
<child :msg1="msg1" :msg2="msg2" title="3333"></child>
<script setup>
import child from './child.vue'
import { ref,reactive } from 'vue
const msg1 = ref('111')
const msg2 = ref('222')
</script>
// ⼦组件
<script setup>
import { defineProps,useContext,useAttars } from 'vue'
const props = defineProps({
msg1: String
})
// ⽅法1
const ctx = useContext()
console.log(ctx.attars) // {msg2:'222',title:'333'}
// ⽅法2
const attrs = useAttars()
console.log(attars)  // {msg2:'2222',title:'3333'}
</script>
v-model
可以⽀持多个数据双向绑定
<child v-model:key="key" v-modle:value="value" />
<script>
import child from './child.vue'
import { ref,reactive } from 'vue'
const key = ref('111')
const value = ref('222')
</script>
//⼦组件
<template>
<button @click="handleClick"></button>
</template>
<script setup>
// ⽅法⼀  v3.2 已被移除
import { useContext } from 'vue'
const { emit } = useContext()
// ⽅法⼆
import { defineEmits } from 'vue'
const emit = defineEmits(['key','value'])
//⽤法
const handleClick = () => {
emit('update:key','新的key')
emit('update:value','新的value')
}
</script>
provide / inject
provide/inject为依赖注⼊ provide:可以让我们指定想要提供给后代组件的数据 inject:在任何后代组件中接受想要添加在这个组件上的数据,不管组件嵌套多深都可以直接拿来⽤
// ⽗组件
<script setup>
import { provide } from 'vue'
const name = provide('name')
console.log('name','沐华')
</script>
//⼦组件
<script setup>
import { inject } from 'vue'
const name = inject('name')
console.log(name) //⽊华
</script>
Vuex
//store/index.js
import { createStore } from 'vuex'
export default createStore({
state:{count:1},
getters:{
getCount:state=&unt
},
mutations:{
add(state){
}
}
})
// main.js
import { createApp } from 'vue'
import APP from './App.vue'
import store from './store'
createApp(APP).use(store).mount("#app")
// 直接使⽤
<template>
<div>
{{ $unt }}
</div>
<button @click="$storemit('add')">
</button>
</template>
// 获取
<script setup>
import { useStore,computed } from 'vuex'
const store = useStore()
console.log(unt)
const count = computed (()=>unt)
console.log(count)
</script>
mitt
Vue3中已经没有了EventBus跨组件通信,替代⽅案mitt.js,但原理⽅式EventBus是⼀样的
安装⽅式 npm i mitt -S
mitt.js
import mitt from 'mitt'
const mitt = mitt()
export default mitt
组件之间使⽤
// 组件A
<script setup>
import mitt from './mitt'
const handleClick = () => {
}
</script>
// 组件B
<script setup>
import mitt from './mitt'
import { onUnmounted } from 'vue'
const someMethod = () => {...}
<('handleChange',someMethod)
onUnmounted(()=>{
mitt.off('handleChange',someMethod)
})
</script>
到此这篇关于vue3组件通信的⽅式总结及实例⽤法的⽂章就介绍到这了,更多相关vue3组件通信的⼏种⽅式内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。