vue非父子组件之间传值

news/2024/7/10 0:31:24 标签: vue, 组件, 非父子组件传值

vue_front__vue_front01_0">代码地址:https://github.com/kedaya-github/vue_front , vue_front01中

组件通信:

  • 父向子,子向父。翻看 vue学习篇–组件,文档。

非父子组件之间

  • 在components中定义一个公共的 bus.js文件,里面封装一个vue对象

      // 创建一个Vue实例
      import Vue from 'vue'
      
      export default new Vue()
    

在这里插入图片描述

  • components——>GG.vue : 两个没有关系的路由组件

      <template>
        <div>
            哥哥组件:
            <input type="button" value="发送信息到 MM" @click="sendMsg()">
        </div>
      </template>
      <script>
      import bus from './bus.js';   //引入 bus.js , 使用bus向 MM 路由组件传值
      export default {
        data() { 
          return {
              ggMsg : "哥哥组件信息"
          }
        },
        methods:{
            sendMsg(){
                bus.$emit("GGMM" , this.ggMsg , "测试多数据");  //调用bus.$emit() 绑定一个事件 , 第二个参数为 要传递的数据
            }
        },
       }
      </script>
    
  • components——>MM.vue : 接收 GG.vue 传递的数据

      <template>
        <div>
            MM组件:
            GG组件的信息 : {{mmMsg}}
        </div>
      </template>
      <script>
      import bus from './bus.js';
      export default {
        data() { 
          return {
              mmMsg : ""
          }
        },
        created(){  //初始化数据时,获取GG组件 传递的数据
              bus.$on("GGMM" , (data,msg) => {   //bus.$on 来获取非父子组件传递的数据  第一个参数为共同绑定的事件  , data为传递的数据, 
                  console.log(msg);  //() 中可以传递多值
                  this.mmMsg = data; 
              })
        }
       }
      </script>
    

总结:

  1. 非父子组件的传值,也可以使用 vuex进行传值。但是使用bus更加规范。
  2. 两个传值的组件,都需要 import bus from “bus.js”,引入 bus对象。使用bus的方法进行传值
  3. 可以传递多值。

http://www.niftyadmin.cn/n/1411027.html

相关文章

SCSF - Part 7 Introduction to Services in the CAB

Introduction Part 6 of this series of articles concluded our discussion of dependency injection in the CAB. part6 总结了我们关于dependency injection 在CAB里的讨论This article and part 8 of the series will discuss services in the CAB in some more detail. T…

oracle 显式游标

2019独角兽企业重金招聘Python工程师标准>>> 1.语法 CURSOR cursor_name [(parameter_name datatype,...)] IS select_statement; --游标关联的SELECT语句,该语句不能使用SELECT...INTO...语句 使用步骤1.声明游标 DECLARE CURSOR cursor_name IS SELECT_STATEMENT…

vuex操作

vuex 实现组件之间数据共享的一种机制&#xff1b; 使用父子传值或兄弟传值&#xff0c;太麻烦了&#xff1b; 有了 vuex ,想要共享数据&#xff0c;只需要把数据挂载到 vuex 就行&#xff1b;想要获取数据&#xff0c;直接从vuex 上拿就行&#xff1b; 当 vuex 中的数据被修…

SCSF - Part 8 Creating and Using Services in the CAB

Introduction Part 7 of this series of articles gave us a general introduction to services in the CAB. This article will go into more detail on the various ways we can create and use such services. part7 给了我们一个大致的关于CAB里的service的介绍&#xff0c…

Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

网址&#xff1a;http://www.cnblogs.com/-Object/p/4896585.html http://blog.csdn.net/qq992817263/article/details/53907480 前言&#xff1a;因为这段时间要接触到DevExpress控件库&#xff0c;而我本身甚至对winform的控件都了解甚少&#xff0c;所以处在学习中&…

Element-UI详解

Element-UI Element&#xff0c;一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库 Element UI是基于Vue 2.0的Element UI 提供一组组件Element UI 提供组件的参考实例, 直接复制 安装ElementUI 通过 vue-cli创建vue项目 安装 element-ui组件 npm i elemen…

SCSF - Part 9 The Command Design Pattern

Introduction Part 8 of this series of articles concluded our discussion of services in the CAB. 系列的Part8总结了我们关于CAB里service的讨论。 This article and part 10 of this series will talk about commands in the CAB. Commands are a neat way of implementi…

vsCode的vue项目中配置style的——css样式代码提示

vue项目中style样式代码提示 一般情况在vue项目中 .vue结尾的文件中&#xff0c;在dom节点上定义style"…" 中书写 css样式时没有代码提示。只能手写就很苦恼。 解决方法&#xff1a; 在打开的 vue组件页面中&#xff0c;右下角修改了 html &#xff0c; 就拥有了…