关于vue之间的组件通讯

1: prop,refs 父组件 => 子组件

1) prop

// app.vue<template><div id="app"><childTest1 :msg="msg"/></div>
</template><script>
import childTest1 from './components/childTest1'export default {name: 'App',data () {return {msg: 'data from parent'}},methods: {},components: {childTest1}
}
</script>

// childTest1.vue
<template><div class="hello">{{msg}}</div>
</template><script>
export default {name: 'childTest1',props: ['msg'],data () {return {msgTest2: null}},methods: {}
}
</script>

2) refs

// app.vue
<template><div id="app"><childTest1 :msg="msg" ref="childTest1"/></div>
</template><script>
import childTest1 from './components/childTest1'export default {name: 'App',data () {return {msg: 'data from parent'}},created () {// 无效this.$refs.childTest1.setMsgTest2('refs set msg test')},mounted () {this.$refs.childTest1.setMsgTest2('refs set msg test')},methods: {},components: {childTest1}
}
</script>

// childTest1.vue<template><div class="hello"><p>{{msg}}</p><p style="color: #f80000">{{msgTest2}}</p></div>
</template><script>
export default {name: 'childTest1',props: ['msg'],data () {return {msgTest2: null}},methods: {setMsgTest2 (val) {this.msgTest2 = val}}
}
</script>

2 : $emit 子组件 => 父组件

// App.vue<template><div id="app"><childTest1 :msg="msg" ref="childTest1" @changeMsg="setMsg"/></div>
</template><script>
import childTest1 from './components/childTest1'export default {name: 'App',data () {return {msg: 'data from parent'}},created () {// 无效this.$refs.childTest1.setMsgTest2('refs set msg test')},mounted () {this.$refs.childTest1.setMsgTest2('refs set msg test')},methods: {setMsg (val) {this.msg = val}},components: {childTest1}
}
</script>

// childTest1.vue
<template><div class="hello"><p>{{msg}}</p><p style="color: #f80000">{{msgTest2}}</p><button @click="changeMsg">changeMsg</button></div>
</template><script>
export default {name: 'childTest1',props: ['msg'],data () {return {msgTest2: null}},methods: {setMsgTest2 (val) {this.msgTest2 = val},changeMsg () {this.$emit('changeMsg', 'child change test msg')}}
}
</script>

上面是简单的组件通讯,简单的父子组件传递可以使用上述形式,当比较复杂的情况下, 或者组件是相互独立,而且中间一个发生变化另一个变化的时候可以使用vue bus。

3: vue bus 独立组件 => 独立组件 (父 => 子 子 => 父)

// app.Vue<template><div id="app">{{parentMsg}}<childTest1 :msg="msg" ref="childTest1" @changeMsg="setMsg"/><ChildTest2 /></div>
</template><script>
import ChildTest1 from './components/ChildTest1'
import ChildTest2 from './components/ChildTest2'
import ChildTestBus from './bus/ChildTest'export default {name: 'App',data () {return {parentMsg: 'parent init msg',msg: 'data from parent'}},created () {ChildTestBus.$on('changeMsg', val => this.setParentMsg(val))// 无效this.$refs.childTest1.setMsgTest2('refs set msg test')},mounted () {this.$refs.childTest1.setMsgTest2('refs set msg test')},destrory () {ChildTestBus.$off('changeMsg')},methods: {setParentMsg (val) {this.parentMsg = val},setMsg (val) {this.msg = val}},components: {ChildTest1,ChildTest2}
}
</script>

// ChildTest1.vue<template><div class="hello"><p>{{msg}}</p><p style="color: #f80000">{{msgTest2}}</p><button @click="changeMsg">changeMsg</button><button @click="changeParentMsg" style="background: #ddd">changeMsgFromTest1</button></div>
</template><script>
import ChildTestBus from '../bus/ChildTest'
export default {name: 'ChildTest1',props: ['msg'],data () {return {msgTest2: null}},methods: {setMsgTest2 (val) {this.msgTest2 = val},changeMsg () {this.$emit('changeMsg', 'child change test msg')},changeParentMsg () {ChildTestBus.$emit('changeMsg', 'change msg from childTest1')}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

// ChildTest2.vue<template><div class="hello"><p style="color: blue">{{msgTest2}}</p><button @click="changeMsg">changeMsgFromTest2</button></div>
</template><script>
import ChildTestBus from '../bus/ChildTest'
export default {name: 'ChildTest2',props: ['msg'],data () {return {msgTest2: 'msg in childTest2'}},methods: {changeMsg () {ChildTestBus.$emit('changeMsg', 'change msg from childTest2')}}
}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

// ChildTest.js

import Vue from 'Vue'
export default new Vue()

上述就可以实现复杂点的组件通讯了,这里有着注意点,当你使用bus时,通过$on事件获取消失时,得在组件注销时解除监听。

这个的缺点时当你的应用比较大时,这个触发和监听信息的代码需要在多处使用,这样我们就需要统一管理了,既便于维护,也方便理解

这个时候就可以用到vuex ,详情下级分解

转载于:https://www.cnblogs.com/mapletao/p/8495077.html

vue: 从组件通讯到vuex (上)相关推荐

  1. Vue 路由组件通讯传参的 8 种方式

    当 props 是一个对象时,它将原样设置为组件 props.当 props 是静态的时候很有用. 我们在开发单页面应用时,有时需要进入某个路由后基于参数从服务器获取数据,那么我们首先要获取路由传递过 ...

  2. vue.js 组件之间传递数据 1

    vue.js 组件之间传递数据 框架 浏览数:437 2017-8-21 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.如何传递数据 ...

  3. 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据

    前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于"父子组件通信"的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰 ...

  4. 在 vue 组件中查看 vuex 定义

    原文:在 vue 组件中查看 vuex 定义 在进行 vue 项目开发的时,如果项目中用到了 vuex 做状态管理,经常需要查看 store 里面定义的状态属性.但是在 vue 组件中引用这些 vue ...

  5. 五分钟带你摸透 Vue组件及组件通讯

    一.组件化开发 组件 (Component) 是 Vue.js 强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代 码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能 ...

  6. 九段刀客:vue组件通讯

    组件通信的方法: 1.props和emit 2.vuex 3.bus 4.children和parent 5.ref props和emit() 说明:props和emit用于父子组件通讯,props父 ...

  7. Vuex---在 Vue 组件中获得 Vuex 状态state

    Vuex使用单一状态树(一个对象就包含了全部的应用层级状态),它作为唯一数据源存在,每个应用仅仅有一个store实例. 单一状态树使得我们能够直接定位任一特定的状态片段,在调试过程中也能轻易地取得整个 ...

  8. 【VUE3】保姆级基础讲解(二)计算属性,vue组件,vue-cli脚手架,组件通讯,插槽slot

    目录 计算属性computed 侦听器watch 对象监听 组件 注册全局组件 注册局部组件 Vue-CLI脚手架 安装和使用 .browserslistrc main.js jsconfig.jso ...

  9. vue父子组件传值 简单了解vuex

    一.vue的父子组件之间是如何传值的? 首先呢,需要说说的是,vue既然有双向绑定,那为何会有父子组件之间的传值问题?这个问题也简单,vue的组件会供其他的vue页面进行调用,如果数组都是双向绑定的话 ...

  10. 在vue组件中使用vuex的state状态对象的5种方式

    下面是store文件夹下的state.js和index.js内容 state.js //state.js const state = {headerBgOpacity:0,loginStatus:0, ...

最新文章

  1. mysql5 数据类型,Mysql学习笔记5-----字段的数据类型
  2. 笔记:基于标签的推荐系统、基于图的推荐算法、PersonalRank
  3. C#中巧用Lambda表达式实现对象list进行截取
  4. android应用js
  5. 城市间紧急救援 (25 分)【dijkstra模板 超时原因】
  6. Longest Palindrome CodeForces - 1304B(思维)
  7. 思科模拟器:[1]安装及汉化详解
  8. kissy 淘宝网脚本库
  9. [转]Python3之max key参数学习记录
  10. java技术分享ppt_精美PPT制作培训 | 技术二部内部分享
  11. C3P0连接池的使用
  12. 电脑备份,电脑怎么备份系统,小白提供2种备份方法
  13. mysql 多条件求和_sql多条件求和-sql条件求和-sql求和且和满足条件
  14. 教你用Python自制拼图小游戏,一起来玩吧
  15. 简述使用混合传递参数时的基本原则_过程控制系统与仪表习题答案 -
  16. php 函数索引 中文索引
  17. Android基础入门教程——10.1 TelephonyManager(电话管理器)
  18. 用el-select处理后台数据进行页面渲染,返回数据如果为空或者无法渲染文字时的处理
  19. 【解决方案】SkeyeVSS视频安防综合管理系统助力解决夜吃烧烤安全隐患,为夜市安全保驾护航
  20. 解决佳能MG3080、MG2980打印机报错5B00无法打印的问题

热门文章

  1. (转)详解HTML网页源码的charset格式
  2. C#对象的浅拷与深拷贝
  3. MutationObserver监听页面是否加载完成
  4. 解决出现“未能加载文件或程序集“System.Net.Http.Formatting, Version=5.2.3.0”的问题
  5. 1. golang 接入Discord做消息推送
  6. MySQL 一条SQL语句执行得很慢的原因有哪些?
  7. 计算机多媒体处理的是什么意思,多媒体处理的是什么信号
  8. ssm整合:这是第三次自己整合了。特写此文章当做笔记
  9. macbook python安装_mac下安装Python3.*(最新版本)
  10. 德国Java工程师_1886年,德国工程师。