Vue3 组件通信

父组件向子组件传值

父组件代码如下:

<script setup lang="ts">
  import { ref } from "vue";
  import AboutView from "./views/AboutView.vue";
  const test=ref('测试')
</script>

<template>
  <AboutView test="test"></AboutView>
</template>

子组件 AboutView.vue 码如下:

<script setup lang="ts">
interface props {
  test:string
}
defineProps<props>()
</script>
<template>
  <div>这是父组件传过来的数据:{{test}}</div>
</template>
子组件向父组件传值

父组件代码如下:

<script setup lang="ts">
  import { ref } from "vue";
  import HomeView from "./views/HomeView.vue";
  const test=ref('测试')
  const getVal=(val:string)=>{
    test.value=val
  }
</script>

<template>
  <HomeView @_click="getVal"></HomeView>
  <div>{{ test }}</div>
</template>

子组件 HomeView.vue 代码如下:

<script setup lang="ts">
  const emit=defineEmits(['_click'])
  const func=()=>{
    emit('_click','我是子组件传过来的数据')
  }
</script>

<template>
  <button @click="func">按钮</button>
</template> 
兄弟组件通信传值

在 Vue3 中,使用 EventBus(事件总线)是一种常见的跨组件通信方式。EventBus 允许组件通过发送和接收事件来传递数据,而不需要直接相互引用。

如下,创建一个 Bus 事件中心,bus.ts:

interface BusClass{
    emit:(funcName:string)=>void
    on:(funcName:string,callBack:Function)=>void
}
type Key=number|string|symbol
interface List{
    [key:Key]:Array<Function>
}
class Bus implements BusClass{
    list:List
    constructor(){
        this.list={}
    }
    emit(funcName: string,...args:any[]){
        this.list[funcName].forEach((fn)=>{
            fn.apply(this,args)
        })
    }
    on(funcName: string, callBack: Function){
        const fn=this.list[funcName]||[]
        fn.push(callBack)
        this.list[funcName]=fn
    }

}
export default new Bus()

创建一个子组件 HomeView.vue,如下:

<script setup lang="ts">
  import Bus from "../utils/bus";
  const func=()=>{
    Bus.emit('_click','你好啊')
  }
</script>

<template>
  <button @click="func">按钮</button>
</template> 

  创建一个子组件 AboutView.vue,如下:

<script setup lang="ts">
import { ref } from "vue";
import Bus from "../utils/bus";
const test=ref('哈哈哈')
Bus.on('_click',(val:string)=>{
  test.value=val
})
</script>
<template>
  <div>{{test}}</div>
</template>

创建一个父组件 parent.vue,并将两个子组件引入,如下:

<script setup lang="ts">
  import HomeView from "./views/HomeView.vue";
  import AboutView from "./views/AboutView.vue";
</script>

<template>
  <HomeView></HomeView>
  <AboutView></AboutView>
</template>

以上就实现了兄弟之间的组件通信。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3的组件通信方式和Vue2有些不同,主要是因为Vue3中使用了Composition API,其中提供了两种方式进行组件通信: 1. Props 和 Emit 这是Vue2中也存在的方式,通过在父组件中向子组件传递props,子组件通过emit事件向父组件传递信息。在Vue3中,可以使用 `defineProps` 和 `defineEmits` 来定义props和emit事件。示例: ```html <!-- Parent.vue --> <template> <Child :msg="message" @send-msg="handleMsg"></Child> </template> <script> import { defineComponent, ref } from 'vue' import Child from './Child.vue' export default defineComponent({ components: { Child }, setup() { const message = ref('Hello') const handleMsg = (msg) => { console.log(`Received message from child: ${msg}`) } return { message, handleMsg } } }) </script> <!-- Child.vue --> <template> <button @click="sendMsg">Send Message</button> </template> <script> import { defineComponent, defineEmits, propType } from 'vue' export default defineComponent({ emits: ['send-msg'], props: { msg: { type: String, required: true } }, setup(props, { emit }) { const sendMsg = () => { emit('send-msg', 'Hello from child') } return { sendMsg } } }) </script> ``` 2. Provide 和 Inject Provide和Inject是Vue3中新增的方式,可以在父组件中提供数据,让子组件中使用。Provide和Inject可以传递任何类型的数据,包括对象、数组、函数等。示例: ```html <!-- Parent.vue --> <template> <div> <h1>{{ title }}</h1> <Child /> </div> </template> <script> import { defineComponent, provide } from 'vue' import Child from './Child.vue' export default defineComponent({ components: { Child }, setup() { provide('title', 'Hello from parent') } }) </script> <!-- Child.vue --> <template> <div> <h2>{{ subtitle }}</h2> </div> </template> <script> import { defineComponent, inject } from 'vue' export default defineComponent({ setup() { const title = inject('title', 'Default title') const subtitle = 'Hello from child' return { title, subtitle } } }) </script> ``` 以上是两种Vue3中常用的组件通信方式,具体使用哪种方式取决于组件之间的关系和需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值