当前位置: 首页 > news >正文

Vue3的多种组件通信方式

 父组件向子组件传递数据 (Props)

父组件

<template><child :name="name"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'const name = ref('小明')
</script>

子组件

<template><div>{{ props.name }}</div>
</template><script setup>
import { defineProps } from 'vue'const props = defineProps({name: {type: String,default: '',},
})
</script>

子组件向父组件传递数据 (Emit)

子组件

<template><button @click="handleClick">点击我</button>
</template><script setup>
import { ref, defineEmits } from 'vue'const message = ref('来自子组件的问候')
const emits = defineEmits(['greet'])const handleClick = () => {emits('greet', message.value)
}
</script>

父组件

<template><child @greet="handleGreet"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'const handleGreet = (message) => {console.log(message) // 输出: "来自子组件的问候"
}
</script>

兄弟组件通信 (Mitt)

npm install --save mitt

mainjs全局配置

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

发送事件组件

<script setup>
import { getCurrentInstance } from 'vue'const { proxy } = getCurrentInstance()
const sendMessage = () => {proxy.$bus.emit('myEvent', '你好,兄弟')
}
</script>

接受事件组件

<script setup>
import { onMounted, getCurrentInstance } from 'vue'const { proxy } = getCurrentInstance()onMounted(() => {proxy.$bus.on('myEvent', (message) => {console.log(message) // 输出: "你好,兄弟"})
})
</script>

透传 Attributes ($attrs)

父组件

<template><child name="小明" age="18" hobby="篮球"></child>
</template>

子组件

<script setup>
import { useAttrs } from 'vue'const attrs = useAttrs()
console.log(attrs) // { age: "18", hobby: "篮球" }
</script>

模板引用 (Refs)

父组件

<template><child ref="childRef"></child><button @click="callChildMethod">调用子组件方法</button>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'const childRef = ref(null)const callChildMethod = () => {childRef.value.someMethod()
}
</script>

子组件

<script setup>
import { defineExpose } from 'vue'const someMethod = () => {console.log('子组件方法被调用了')
}defineExpose({someMethod,
})
</script>

双向绑定 (v-model)

父组件

<template><child v-model:name="name"></child>
</template><script setup>
import { ref } from 'vue'
import Child from './Child.vue'const name = ref('小明')
</script>

子组件

<template><input :value="name" @input="updateName" />
</template><script setup>
import { defineProps, defineEmits } from 'vue'const props = defineProps(['name'])
const emit = defineEmits(['update:name'])const updateName = (e) => {emit('update:name', e.target.value)
}
</script>

依赖注入 (Provide/Inject)

爷组件

<script setup>
import { provide, ref } from 'vue'const themeColor = ref('blue')
provide('theme', themeColor)
</script>

子孙组件

<script setup>
import { inject } from 'vue'const theme = inject('theme')
console.log(theme.value) // 'blue'
</script>

路由传参

通过query

import { useRouter } from 'vue-router'const router = useRouter()
router.push({ path: '/user', query: { id: 123 } })// 在目标组件中
import { useRoute } from 'vue-router'const route = useRoute()
console.log(route.query.id) // 123

Vuex 状态管理

// store/index.js
import { createStore } from 'vuex'export default createStore({state: {count: 0,},mutations: {increment(state) {state.count++},},
})// 在组件中使用
import { useStore } from 'vuex'const store = useStore()
console.log(store.state.count)
store.commit('increment')

Pinia 状态管理

// stores/counter.js
import { defineStore } from 'pinia'export const useCounterStore = defineStore('counter', {state: () => ({ count: 0 }),actions: {increment() {this.count++},},
})// 在组件中使用
import { useCounterStore } from '@/stores/counter'const counter = useCounterStore()
console.log(counter.count)
counter.increment()

浏览器存储

localStorage 和 sessionStorage 可以用于在不同页面或组件之间共享数据。

// 存储数据
localStorage.setItem('user', JSON.stringify({ name: '小明', age: 18 }))// 读取数据
const user = JSON.parse(localStorage.getItem('user'))

Window 对象

// 设置全局数据
window.globalData = { message: '全局消息' }// 在任何地方使用
console.log(window.globalData.message)

 全局属性

Vue 3 提供了 app.config.globalProperties 来替代 Vue 2 中的 Vue.prototype,用于添加全局可用的属性。

// main.js
const app = createApp(App)
app.config.globalProperties.$http = axios// 在组件中使用
import { getCurrentInstance } from 'vue'const { proxy } = getCurrentInstance()
proxy.$http.get('/api/data')

http://www.lryc.cn/news/425334.html

相关文章:

  • 【C++语言】list的构造函数与迭代器
  • Python 安装 PyTorch详细教程
  • html页面缩放自适应
  • 024.自定义chormium-修改屏幕尺寸
  • 测试环境搭建整套大数据系统(十九:kafka3.6.0单节点做 sasl+acl)
  • 小白零基础学数学建模应用系列(五):任务分配问题优化与求解
  • 怎么防止源代码泄露?十种方法杜绝源代码泄密风险
  • uniapp left right 的左右模态框
  • Docker Compose与私有仓库部署
  • Layout 布局组件快速搭建
  • 北京城市图书馆-非遗文献馆:OLED透明拼接屏的璀璨应用
  • OpenCV图像滤波(12)图像金字塔处理函数pyrDown()的使用
  • css如何使一个盒子水平垂直居中
  • 机器人等方向学习和研究的目标
  • 封装一个细粒度的限流器
  • 【Spring Boot - 注解】@ResponseBody 注解:处理 JSON 响应
  • 无人机航拍与ArcGIS融合实战:从地表观测到空间数据可视化的全方位指南!无人机图像拼接数据处理与分析、可视化与制图
  • 日期转时间濯
  • 【计算机网络】TCP实战
  • 使用Python制作贪吃蛇小游戏
  • 线程的退出
  • 【AI 绘画】Q版人物定制生成
  • Python爬虫——爬取某网站的视频
  • Android逆向题解攻防世界-easy-apk
  • Linux系统使用Typecho搭建个人网站并一键发布公网远程管理本地站点
  • 机器学习速成第三集——无监督学习之聚类(理论部分)!
  • 【机器学习】CNN的基本架构模块
  • 第八节AWK报告生成器(2)
  • Linux 进程间通信之管道
  • IDEA 无法启动,点击之后没有任何提示或者界面