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

Vue2中组件的通信方式总结

文章目录

  • 前言
  • 一、为什么组件之间需要通信
  • 二、父子通信
    • 1.Props(父传子)
    • 2.$emit+自定义属性(子传父)
    • 三、兄弟组件通信
      • 通过共同的父组件(Event Bus 模式)
    • 四、跨层级组件通信
      • Provide / Inject
    • 五、Vuex 状态管理
      • Store 配置
      • 在组件中使用
    • 六、5. 其他通信方式
      • $refs
      • $parent / $children
      • $attrs / $listeners
  • 总结


前言

在复习vue2的过程中,我发现我对组件之间的通信方式的知识点有所遗忘,写一篇博客总结一下


一、为什么组件之间需要通信

组件通信就是组件和组件之间的数据传递,因为组件和组件之间的数据是独立的,无法直接访问其他组件的数据,想用其他组建的数据需要用到组件通信。

二、父子通信

1.Props(父传子)

<!-- 父组件 -->
<template><ChildComponent :message="parentMessage" :count="count" />
</template><script>
export default {data() {return {parentMessage: 'Hello from parent',count: 0}}
}
</script><!-- 子组件 -->
<template><div><p>{{ message }}</p><p>{{ count }}</p></div>
</template><script>
export default {props: {message: String,count: Number}
}
</script>

2.$emit+自定义属性(子传父)

<!-- 子组件 -->
<template><button @click="sendMessage">发送消息给父组件</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('message-from-child', 'Hello from child!')}}
}
</script><!-- 父组件 -->
<template><ChildComponent @message-from-child="handleChildMessage" />
</template><script>
export default {methods: {handleChildMessage(message) {console.log(message) // 'Hello from child!'}}
}
</script>

三、兄弟组件通信

通过共同的父组件(Event Bus 模式)

// event-bus.js
import Vue from 'vue'
export const EventBus = new Vue()// 组件 A - 发送事件
EventBus.$emit('data-shared', data)// 组件 B - 接收事件
EventBus.$on('data-shared', (data) => {// 处理数据
})

四、跨层级组件通信

Provide / Inject

<!-- 祖先组件 -->
<template><div><ParentComponent /></div>
</template><script>
export default {provide() {return {theme: 'dark',user: {name: 'John',age: 25}}}
}
</script><!-- 后代组件 -->
<template><div :class="`theme-${theme}`">{{ user.name }}</div>
</template><script>
export default {inject: ['theme', 'user']
}
</script>

五、Vuex 状态管理

Store 配置

// store.js
import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({state: {count: 0,user: null},mutations: {increment(state) {state.count++},setUser(state, user) {state.user = user}},actions: {login({ commit }, user) {commit('setUser', user)}},getters: {isLoggedIn: state => !!state.user}
})

在组件中使用

<template><div><p>{{ count }}</p><p>{{ isLoggedIn }}</p><button @click="increment">增加</button><button @click="login">登录</button></div>
</template><script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'export default {computed: {...mapState(['count']),...mapGetters(['isLoggedIn'])},methods: {...mapMutations(['increment']),...mapActions(['login'])}
}
</script>

六、5. 其他通信方式

$refs

<template><ChildComponent ref="child" /><button @click="callChildMethod">调用子组件方法</button>
</template><script>
export default {methods: {callChildMethod() {this.$refs.child.someMethod()}}
}
</script>

$parent / $children

// 访问父组件
this.$parent.parentMethod()// 访问子组件
this.$children[0].childMethod()

$attrs / $listeners

<!-- 中间组件 -->
<template><GrandChild v-bind="$attrs" v-on="$listeners" />
</template><script>
export default {inheritAttrs: false
}
</script>

总结

组件通信是vue中常用的一个知识点,我们需要熟练掌握它。

http://www.dtcms.com/a/462027.html

相关文章:

  • 建网站需要什么资料临淄58同城招聘信息网
  • 学校网站建设配套制度网站基础三要素
  • 湘潭公司做网站建筑施工企业
  • 【After Anaconda installation,conda can not run】
  • 解码Linux基础命令
  • 【docker部署迅雷】
  • 网站建设 天津wordpress 表说明
  • 镇江京口发布seo课程培训课程
  • 网站名称能用商标做名称吗成都较出名的广告公司
  • 笨笨网站建设专家济南中企动力怎么样
  • 东昌府区网站建设公司网站受攻击
  • 电子电气架构 --- 智能汽车操作系统
  • vue 的 ref和 reactive 的区别
  • 深圳网站设计兴田德润放心安卓软件开发自学教程
  • 口碑好的网站建设公司wordpress 3.0
  • 颍上县建设局网站网址导航总是自动在桌面
  • leetcode 83. 删除排序链表中的重复元素
  • leetcode 529 扫雷游戏
  • 怎么用视频做网站背景外贸网站联系方式模板免费
  • 等保二级机房安全环境审计要点
  • phpcve篇
  • 长春网站设计制作培训押注网站建设
  • 蜘蛛爬网站深入理解wordpress
  • 做电影平台网站怎么赚钱吗在线制作网站 如何禁止蜘蛛收录网站
  • KAG: Boosting LLMs in Professional Domains via Knowledge Augmented Generation
  • 商城网站建设案例app门户网站
  • 利用R语言绘制散点图
  • 宁波北仑做网站团购网站怎么做
  • 【SQLite 库】sqlite3_open_v2
  • 无人机巡检光伏电站红外异常检测研究辅导+落地