关于vue3:Vue3的组件通信

1. props

用 props 传数据给子组件有两种办法,如下

办法一:混合写法

// Parent.vue 传送
<child :msg1="msg1" :msg2="msg2"></child>
<script>
import child from "./child.vue"
import { ref, reactive } from "vue"
export default {
    data(){
        return {
            msg1:"这是传级子组件的信息1"
        }
    },
    setup(){
        // 创立一个响应式数据

        // 写法一 实用于根底类型  ref 还有其余用途,上面章节有介绍
        const msg2 = ref("这是传级子组件的信息2")

        // 写法二 实用于简单类型,如数组、对象
        const msg2 = reactive(["这是传级子组件的信息2"])

        return {
            msg2
        }
    }
}
</script>

// Child.vue 接管
<script>
export default {
  props: ["msg1", "msg2"],// 如果这行不写,上面就接管不到
  setup(props) {
    console.log(props) // { msg1:"这是传给子组件的信息1", msg2:"这是传给子组件的信息2" }
  },
}
</script>

办法二:纯 Vue3 写法(语法糖)

// Parent.vue 传送
<child :msg2="msg2"></child>
<script setup>
    import child from "./child.vue"
    import { ref, reactive } from "vue"
    const msg2 = ref("这是传给子组件的信息2")
    // 或者简单类型
    const msg2 = reactive(["这是传级子组件的信息2"])
</script>

// Child.vue 接管
<script setup>
    // 不须要引入 间接应用
    // import { defineProps } from "vue"
    const props = defineProps({
        // 写法一
        msg2: String
        // 写法二
        msg2:{
            type:String,
            default:""
        }
    })
    console.log(props) // { msg2:"这是传级子组件的信息2" }
</script>

**留神:

如果父组件是混合写法,子组件纯 Vue3 写法的话,是接管不到父组件里 data 的属性,只能接管到父组件里 setup 函数里传的属性

如果父组件是纯 Vue3 写法,子组件混合写法,能够通过 props 接管到 data 和 setup 函数里的属性,然而子组件要是在 setup 里接管,同样只能接管到父组件中 setup 函数里的属性,接管不到 data 里的属性

官网也说了,既然用了 3,就不要写 2 了,所以不举荐混合写法。上面的例子,一律只用纯 Vue3 的写法,就不写混合写法了**

2. $emit

    // Child.vue 派发
    <template>
        <button @click="handleClick">按钮</buttom>
    </template>
    <script setup>
        const emit = defineEmits(["myClick","click2"])
        //emits 为 defineEmits 显示申明后的对象。
         //如存在多个监听事件则为 defineEmits(["myClick","click2"])
        // 对应写法二
        const handleClick = ()=>{
            emit("myClick", "这是发送给父组件的信息")
            emit("click2", "这是发送给父组件的信息2222222222222")
        }
    </script>
    // Parent.vue 响应
    <template>
        <child @myClick="onMyClick" @click2="click2My"></child>
    </template>
    <script setup>
        import child from "./child.vue"
        const onMyClick =(msg)=>{
          console.log(msg,'666666666') //这是发送给父组件的信息
        }
        const click2My= (msg)=>{
          console.log(msg,'999999999') //这是发送给父组件的信息2222222222222
        }
    </script>

3. ref

父组件获取子组件的属性或者调用子组件办法

// Child.vue
<script setup>
    // import { defineExpose } from "vue"
    defineExpose({
        childName: "这是子组件的属性",
        someMethod(){
            console.log("这是子组件的办法")
        }
    })
</script>

// Parent.vue  留神 ref="comp"
<template>
    <child ref="comp"></child>
    <button @click="handlerClick">按钮</button>
</template>
<script setup>
    import child from "./child.vue"
    import { ref } from "vue"
    const comp = ref(null)
    const handlerClick = () => {
        console.log(comp.value.childName) // 获取子组件对外裸露的属性
        comp.value.someMethod() // 调用子组件对外裸露的办法
    }
</script>

4.attrs

attrs:蕴含父作用域里除 class 和 style 除外的非 props 属性汇合

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

  // Child.vue 接管
  <script setup>
      import { defineProps, useContext, useAttrs } from "vue"
      // 3.2版本不须要引入 defineProps,间接用
      const props = defineProps({
          msg1: String
      })
      // 办法一 不适用于 Vue3.2版本,该版本 useContext()已废除
      const ctx = useContext()
      // 如果没有用 props 接管 msg1 的话就是 { msg1: "1111", msg2:"2222", title: "3333" }
      console.log(ctx.attrs) // { msg2:"2222", title: "3333" }

      // 办法二 实用于 Vue3.2版本
      const attrs = useAttrs()
      console.log(attrs) // { msg2:"2222", title: "3333" }
  </script>

5.v-model

 // Parent.vue
<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>

// Child.vue
<template>
    <button @click="handlerClick">按钮</button>
</template>
<script setup>

    // 办法一  不适用于 Vue3.2版本,该版本 useContext()已废除
    import { useContext } from "vue"
    const { emit } = useContext()

    // 办法二 实用于 Vue3.2版本,不须要引入
    // import { defineEmits } from "vue"
    const emit = defineEmits(["key","value"])

    // 用法
    const handlerClick = () => {
        emit("update:key", "新的key")
        emit("update:value", "新的value")
    }
</script>

6. provide / inject

rovide / inject 为依赖注入
provide:能够让咱们指定想要提供给后辈组件的数据或
inject:在任何后辈组件中接管想要增加在这个组件上的数据,不论组件嵌套多深都能够间接拿来用

// Parent.vue
<script setup>
    import { provide } from "vue"
    provide("name", "王五")
</script>

// Child.vue
<script setup>
    import { inject } from "vue"
    const name = inject("name")
    console.log(name) // 王五
</script>

7. 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>

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据