Vue3.2版本组件通讯

Vue3.2版本组件通讯

      • 一、`props`
      • 二、`$emit`
      • 三、`expose / ref` (父组件获取子组件的属性或者调用子组件方法)
      • 四、`attrs`(包含父作用域里除 class 和 style 除外的非 props 属性集合)
      • 五、`v-model`(可以支持多个数据双向绑定)
      • 六、`provide / inject`(provide / inject 为依赖注入)
      • 七、`Vuex`

注意

  • 本文方法适用于Vue3.2及以上的版本。Vue3.2以下版本请自行脑补。

一、props

// 父页面传送
<child :msg="msg"></child>
<script setup>
    import child from "./child.vue"
    import { ref, reactive } from "vue"
    const msg = ref("父组件传给子组件的信息")
    // 或者复杂类型 reactive(["父组件传给子组件的信息"]) 
</script>

// 子页面接收
<script setup>
    // import { defineProps } from "vue"
    // 注:defineProps 不需要引入可以直接使用
    const props = defineProps({
        msg:{
            type:String,
            default:""
        }
    })
    console.log(props) // { msg:"父组件传给子组件的信息" }
</script>

二、$emit

// 子组件派发
<template>
    <button @click="childClick">按钮</buttom>
</template>
<script setup>
	// import {defineEmits} from 'vue'
	// 注:defineEmits 不需要引入可以直接使用
	const emit = defineEmits(['myClick']); // 必须要声明传递的事件名,更多用‘,’分割
    const childClick = ()=>{
        emit("myClick", "子组件发送给父组件的信息")
    }
</script>

// 父组件响应
<template>
    <child @myClick="parentClick"></child>
</template>
<script setup>
    import child from "./child.vue"
    const parentClick = (msg) => {
        console.log(msg) // 子组件发送给父组件的信息
    }
</script>

三、expose / ref (父组件获取子组件的属性或者调用子组件方法)

// 子组件
<script setup>
    // import { defineExpose } from "vue"
    // 注:defineExpose 不需要引入可以直接使用
    defineExpose({
        childName: "这是子组件的属性",
        someMethod(){
            console.log("这是子组件的方法")
        }
    })
</script>

// 父组件  注意 ref="pare"
<template>
    <child ref="pare"></child>
    <button @click="handlerClick">按钮</button>
</template>
<script setup>
    import child from "./child.vue"
    import { ref } from "vue"
    const pare = ref(null)
    const handlerClick = () => {
        console.log(pare.value.childName) // 获取子组件对外暴露的属性
        pare.value.someMethod() // 调用子组件对外暴露的方法
    }
</script>

四、attrs(包含父作用域里除 class 和 style 除外的非 props 属性集合)

// 父组件传送
<child :msg1="msg1" :msg2="msg2" title="3333"></child>
<script setup>
    import child from "./child.vue"
    import { ref } from "vue"
    const msg1 = ref("1111")
    const msg2 = ref("2222")
</script>

// 子组件接收
<script setup>
    import { useAttrs } from "vue"
    // 注:defineProps 不需要引入可以直接使用
    const props = defineProps({
        msg1: String
    })
    const attrs = useAttrs()
    console.log(attrs) // { msg2:"2222", title: "3333" }
</script>

拓展useSlots获取插槽数据。

五、v-model(可以支持多个数据双向绑定)

// 父组件
<child v-model:key="key" v-model:value="value"></child>
<script setup>
    import child from "./child.vue"
    import { ref, reactive } from "vue"
    const key = ref("1111")
    const value = ref("2222")
</script>

// 子组件
<template>
    <button @click="handlerClick">按钮</button>
</template>
<script setup>
    const emit = defineEmits(["key","value"])
    const handlerClick = () => {
        emit("update:key", "新的key") // 1111
        emit("update:value", "新的value") // 2222
    }
</script>

六、provide / inject(provide / inject 为依赖注入)

provide:可以让我们指定想要提供给后代组件的数据或
inject:在任何后代组件中接收想要添加在这个组件上的数据,不管组件嵌套多深都可以直接拿来用

// 父组件
<script setup>
    import { provide } from "vue"
    provide("name", "Yiu")
</script>

// 子组件
<script setup>
    import { inject } from "vue"
    const name = inject("name")
    console.log(name) // Yiu
</script>

七、Vuex

// store/index.js
import { createStore } from "vuex"
export default createStore({
    state:{ count: 1 },
    getters:{
        getCount: state => state.count
    },
    mutations:{
        add(state){
            state.count++
        }
    }
})

// main.js
import { createApp } from "vue"
import App from "./App.vue"
import store from "./store"
createApp(App).use(store).mount("#app")

// Page.vue
// 方法一 直接使用
<template>
    <div>{{ $store.state.count }}</div>
    <button @click="$store.commit('add')">按钮</button>
</template>

// 方法二 获取
<script setup>
    import { useStore, computed } from "vuex"
    const store = useStore()
    console.log(store.state.count) // 1

    const count = computed(()=>store.state.count) // 响应式,会随着vuex数据改变而改变
    console.log(count) // 1 
</script>

拓展:由于Vue3 中没有了 EventBus 跨组件通信,但是现在有了一个替代的方案 mitt.js,原理还是 EventBus。安装:npm i mitt -S

你可能感兴趣的:(Vue,3,JavaScript,前端的那些事,vue.js,javascript,npm,前端框架,前端)