vue3的兄弟传参(三种方法)

简介: vue3的兄弟传参(三种方法)

1.兄弟A先给父元素 父元素再给子组件B (vue2的思路)
A组件

<template> 
    <div style="width:300px;height:200px;background:blue"> 
        <button @click="add">A派发事件</button> 
    </div>
</template>
<script setup lang="ts">
import { defineEmits } from "vue" 
// emit
const emit=defineEmits(['onclick']);
let  flag=false;
const add=()=>{
    flag=!flag;
  emit('onclick',flag)
}
 
</script>

<style scoped>

</style>

父组件

<template>
  <div>
    <A  @onclick="onclick"></A>
    //在把A组件传输的值传给组件B
    <B :flag='flag'></B>
    A组件传输的值{{flag}}
  </div>
</template>

<script setup lang="ts">
import  {ref} from 'vue'
import  A from './components/fuzi/A.vue'
import  B from './components/fuzi/B.vue'
let  flag=ref(null);
const onclick=(params:boolean)=>{
//拿到传输的值赋给变量flag
flag.value=params;

}

</script>

<style scoped>

</style>

组件B

<template>
    <div style="width:300px;height:200px;background:red"> 
      B组件接受的值:  {{flag}}
    </div>
</template>

<script setup lang="ts">
import { defineProps} from "vue";
import  mitts  from '../../util/bus.js';
 
type Props={
    flag:boolean
}
defineProps<Props>();
</script>

<style scoped>

</style>

2.兄弟组件直接传输 (局部引入)

npm install mitt

定义一个bus.js

import  mitt  from 'mitt';
const  mitts=mitt();
export default mitts;

组件A:

<template> 
    <div style="width:300px;height:200px;background:blue">
        <button @click="child">兄弟传参</button>
    </div>
</template>
<script setup lang="ts">
import  mitts  from '../../util/bus.js'; 
let  flag=false;
const  child=()=>{
    mitts.emit('event',flag)
}
</script>

<style scoped>

</style>

组件B:

<template>
    <div style="width:300px;height:200px;background:red"> 
      B组件接受的值:  {{flag}}
    </div>
</template>

<script setup lang="ts">
import {onBeforeMount,ref} from "vue";
import  mitts  from '../../util/bus.js';
 let  flag=ref(null);
onBeforeMount(()=>{
  mitts.on('event',(e:boolean)=>{
      flag.value=e
  })
})
</script>

<style scoped>

</style>

3.兄弟传参(全局引入)

npm install mitt

main.ts

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import mitt from 'mitt'
const  app=createApp(App);
app.config.globalProperties.$mitt=mitt();
app.mount('#app')

组件A

<template> 
    <div style="width:300px;height:200px;background:blue"> 
        <button @click="child">兄弟传参</button>
    </div>
</template>
<script setup lang="ts">
import {  getCurrentInstance,ComponentInternalInstance  } from "vue" 
const {appContext }=getCurrentInstance() as ComponentInternalInstance
let  flag=false;
 const  child=()=>{
    flag=!flag; 
    appContext.config.globalProperties.$mitt.emit('event',flag) 
}
</script>

<style scoped>

</style>

兄弟B

<template>
    <div style="width:300px;height:200px;background:red"> 
     {{flag}}
    </div>
</template>

<script setup lang="ts">
import { ref ,onBeforeMount,getCurrentInstance,ComponentInternalInstance} from "vue";
type Props={
    flag:boolean
} 
const flag<Props>=ref(null);
const {appContext}=getCurrentInstance() as ComponentInternalInstance 
onBeforeMount(()=>{
    appContext.config.globalProperties.$mitt.on('event',(e:boolean)=>{ 
      flag.value=e;

  })
 
</script>

<style scoped>

</style>
相关文章
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
1430 139
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
751 1
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
1346 11
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
838 0
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
1320 1
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
966 2
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
861 137
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
1981 0
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
2040 78