Home > Web Front-end > Vue.js > body text

How to communicate between Vue components? Several ways of component communication

青灯夜游
Release: 2022-08-12 20:30:24
forward
6277 people have browsed it

VueHow to communicate between components? The following article will introduce you to the communication method of Vue components. I hope it will be helpful to you!

How to communicate between Vue components? Several ways of component communication

#The two major features of vue are responsive programming and componentization. Component is the core function of Vue, but the scope of each component instance is independent of each other, which means that data between different components cannot directly reference each other. If you want to reference data across components, you need to use component communication. Before communicating, you must first understand the relationship between components:

How to communicate between Vue components? Several ways of component communication

As shown in the figure above:
Father-son relationship: A and B, A and C, B and D, C and E
Brother relationship: B and C
Intergenerational relationship (maybe more generations apart): A and D, A and E
Level relationship: B and E, D and E, etc.

Communication method

1.props/$emit

The parent component binds a custom property through v-bind, and the child component receives the data from the parent component through props; the child component The component triggers the event through $emit, and the parent component uses on() or uses v-on on the custom tag of the child component to listen for events triggered by the child component. Customize events to receive data from subcomponents. (Learning video sharing: vue video tutorial)

1. Parent component passes value to child component

The following uses an example to illustrate the parent component Pass the value to the child component. The parent component parent.vue passes the data books:['JavaScript Advanced Programming', 'CSS New World', 'Illustrated HTTP Color Edition'] to the child component child.vue. And displayed in child.vue

// 父组件parent.vue
<template>
  <div>
    <child></child>
  </div>
</template>

<script>
import Child from &#39;./components/Child.vue&#39;

export default {
  name: &#39;parent&#39;,
  components: {
    Child
  },
  data() {
    return {
      books: [&#39;JavaScript高级程序设计&#39;, &#39;CSS新世界&#39;, &#39;图解 HTTP 彩色版&#39;]
    }
  }
}
</script>
Copy after login
// 子组件child.vue
<template>
  <div>
    <ul>
      <li>{{item}}</li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    books: {
      type: Array,
      default: () => {
        return []
      }
    }
  }
}
</script>
Copy after login

How to communicate between Vue components? Several ways of component communication
Note: Passing data through props is one-way. When the parent component data changes, it will be passed to the child component, but the child component cannot modify the props. The passed data is used to modify the corresponding state of the parent component, which is the so-called one-way data flow.

2. The child component passes the value to the parent component

Next, click the book list through the child component, trigger it with $emit(), and then get it from the parent component

// 子组件child.vue
<template>
  <div>
    <ul>
      <li>{{item}}</li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    books: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    like(item) {
      this.$emit(&#39;likeBook&#39;, item)
    }
  }
}
</script>
Copy after login
// 父组件parent.vue
<template>
  <div>
    <child></child>
  </div>
</template>

<script>
import Child from &#39;./components/Child.vue&#39;

export default {
  name: &#39;parent&#39;,
  components: {
    Child
  },
  data() {
    return {
      books: [&#39;JavaScript高级程序设计&#39;, &#39;CSS新世界&#39;, &#39;图解 HTTP 彩色版&#39;]
    }
  },
  methods: {
    likeBook(val) {
      alert(&#39;我最喜欢的书籍是《&#39; + val + &#39;》&#39;)
    }
  }
}
</script>
Copy after login

How to communicate between Vue components? Several ways of component communication

2. &dollar;parent/$children

  • $parent: access the parent component instance
  • $children: access child component instances
// 父组件parent.vue
<template>
  <div>
    <child></child>
    <button>获取子组件数据</button>
  </div>
</template>

<script>
import Child from &#39;./components/Child.vue&#39;

export default {
  name: &#39;parent&#39;,
  components: {
    Child
  },
  data() {
    return {
      books: [&#39;JavaScript高级程序设计&#39;, &#39;CSS新世界&#39;, &#39;图解 HTTP 彩色版&#39;]
    }
  },
  methods: {
    getChildData() {
      alert(this.$children[0].msg)
    }
  }
}
</script>
Copy after login
// 子组件child.vue
<template>
  <div>
    <ul>
      <li>{{item}}</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: &#39;child&#39;,
  data() {
    return {
      bookLists: [],
      msg: &#39;我是子组件的值!&#39;
    }
  },
  mounted() {
    this.bookLists = this.$parent.books
  }
}
</script>
Copy after login

Note: &dollar;parent gets the object, if there is no parent component at the top level What you get is undefined; what you get in $children is an array. If there are no sub-components on the bottom layer, you get an empty array; these two communication methods only Can be used for parent-child component communication

3.ref

#If ref is used on a normal Dom element, the reference points to the DOM element; if used on a child component, The reference points to the component instance, and the component's methods and data can be directly called through the instance

// 父组件parent.vue
<template>
  <div>
    <child></child>
    <button>获取子组件数据</button>
  </div>
</template>

<script>
import Child from &#39;./components/Child.vue&#39;

export default {
  name: &#39;parent&#39;,
  components: {
    Child
  },
  methods: {
    getChildData() {
      const msg = this.$refs[&#39;child&#39;].msg
      console.log(msg)
      this.$refs[&#39;child&#39;].say()
    }
  }
}
</script>
Copy after login
// 子组件child.vue
<script>
export default {
  name: &#39;child&#39;,
  data() {
    return {
      msg: &#39;我是子组件的值!&#39;
    }
  },
  methods: {
    say() {
      alert(&#39;你好,我是子组件!&#39;)
    }
  },
}
</script>
Copy after login

4. provide/inject

The ancestor component passes ##provide to provide variables, descendant components inject variables through inject to obtain the data of ancestor components. No matter how deeply the descendant components are nested, as long as inject is called, the data in provide can be injected. . The following is the specific code:

// 父组件
<template>
  <div>
    <h1>康熙</h1>
    <son></son>
  </div>
</template>

<script>
import Son from &#39;./components/Son.vue&#39;

export default {
  components: {
    Son
  },
  provide() {
    return {
      FatherToSon: this.FatherToSon,
      FatherToGrandson: this.FatherToGrandson,
    }
  },
  data() {
    return {
      FatherToSon: &#39;我是康熙,雍正,你是我儿子!&#39;,
      FatherToGrandson: &#39;我是康熙,乾隆,你是我孙子!&#39;,
    }
  }
}
</script>
Copy after login
// 子组件
<template>
  <div>
    <h1>雍正</h1>
    <button>接收</button>
    <grandson></grandson>
  </div>
</template>

<script>
import Grandson from &#39;./Grandson.vue&#39;
export default {
  components: { 
    Grandson 
  },
  inject: [&#39;FatherToSon&#39;],
  methods: {
    receive() {
      alert(this.FatherToSon)
    }
  }
}
</script>
Copy after login
// 孙组件
<template>
  <div>
    <h1>乾隆</h1>
    <button>接收</button>
  </div>
</template>

<script>
export default {
  inject: [&#39;FatherToGrandson&#39;],
  methods: {
    receive() {
      alert(this.FatherToGrandson)
    }
  }
}
</script>
Copy after login

How to communicate between Vue components? Several ways of component communicationNote: provide/inject can only pass values ​​from top to bottom, and is not responsive. If you want to become a responsive data provide, you need to provide a function

5.

eventBus&dollar;emit/$on

eventBus is a way of message passing, based on a The message center, the mode of subscribing and publishing messages, is called the publish-subscriber mode.

eventBus is also called event bus. In Vue, eventBus can be used as a communication bridge concept. It is like all components share the same event center and can register to send events or receive events to the center, so components can notify other components in parallel up and down.

  • $emit('name',args): name:发布的消息名称 , args:发布的消息
  • $on('name',fn): name:订阅的消息名称, fn: 订阅的消息
  • &dollar;once('name',fn): name:订阅的消息名称, fn: 订阅的消息。与$on相似但是只触发一次,一旦触发之后,监听器就会被移除
  • &dollar;off('name',callback):name:事件名称,callback:回调监听器
    eventbus可以实现任何组件之前的通信,下面以兄弟组件为例

    1、初始化,全局引入

// main.js
// 全局添加事件总线
Vue.prototype.$bus = new Vue()
Copy after login

2、发送事件

在parent.vue引入ChildA和ChildB组件,使它们成为兄弟组件

// 父组件parent.vue
<template>
  <div>
    <childa></childa>
    <childb></childb>
  </div>
</template>

<script>
import ChildA from &#39;./components/childA&#39;
import ChildB from &#39;./components/childB&#39;
export default {
  components: {
    ChildA,
    ChildB
  }
}
</script>
Copy after login

在ChildA组件中用$emit发送事件

// ChildA组件
<template>
  <div>
    <h1>组件A</h1>
    <button>发送</button>
  </div>
</template>

<script>
export default {
  methods: {
    // 发送事件
    send() {
      this.$bus.$emit(&#39;message&#39;, &#39;欢迎使用eventBus!&#39;)
    }
  }
}
</script>
Copy after login

3、接收事件发送的事件

在ChildB组件中用$on接收ChildA发送的事件

// ChildB组件
<template>
  <div>
    <h1>组件B</h1>
  </div>
</template>

<script>
export default {
  mounted() {
    // 接收事件
    this.$bus.$on(&#39;message&#39;, data => {
      alert(&#39;我是组件B,我收到的消息为:&#39; + data)
    })
  },
  beforeDestroy() {
    this.$bus.$off(&#39;message&#39;)
  }
}
</script>
Copy after login

注意:&dollar;on监听的事件不会自动移除监听,因此在不用时最好使用$off移除监听以免产生问题

六、&dollar;attrs/$listeners

1、简介

当组件为两级嵌套时,一般采用props&dollar;emit,但遇到多级组件嵌套时这种方法就不太适用了,如果不做中间处理,只传递数据用How to communicate between Vue components? Several ways of component communication有点大材小用了。因此在vue2.4中为了解决这一需求,便引入了&dollar;attrs$listeners, 新增了inheritAttrs属性

  • &dollar;attrs:当父组件传递了很多数据给子组件时,子组件没有声明props来进行接收,么子组件中的attrs属性就包含了所有父组件传来的数据(除开已经props声明了的);子组件还可以使用v−bind="$attrs"的形式将所有父组件传来的数据(除开已经props声明了的)传向下一级子组件,通常和interitAttrs属性一起使用。
  • &dollar;listeners:包含了父组件中(不含.native修饰器的)v-on 事件监听器,通过v-on="$listeners",可以将这些事件绑定给它自己的子组件

    2、实例

    下面看一个例子:

// 父组件
<template>
  <div>
    <childa></childa>
  </div>
</template>

<script>
import ChildA from &#39;./components/childA&#39;
export default {
  name: &#39;parent&#39;,
  components: {
    ChildA,
  },
  data() {
    return {
      name: &#39;小明&#39;,
      age: 18,
      sex: &#39;男&#39;
    }
  },
  methods: {
    // 获取名字
    getName() {
      console.log(&#39;我的名字是&#39; + this.name)
    },
    // 获取年龄
    getAge() {
      console.log(&#39;我今年&#39; + this.age + &#39;岁&#39;);
    }
  }
}
</script>
Copy after login
// 子组件A
<template>
  <div>
    <h1>组件A</h1>
    {{ msgA }}
    <hr>
    <childb></childb>
  </div>
</template>

<script>
import ChildB from &#39;./childB.vue&#39;
export default {
  name: &#39;ChildA&#39;,
  components: {
    ChildB
  },
  data() {
    return {
      msgA: null,
      height: &#39;175cm&#39;
    }
  },
  props: {
    sex: {
      type: String,
      default: &#39;&#39;
    }
  },
  mounted() {
    this.msgA = this.$attrs
    console.log(&#39;组件A获取的$listeners:&#39;, this.$listeners)
  },
  methods: {
    // 获取身高
    getHeight() {
      console.log(&#39;我的身高是&#39; + this.height);
    }
  }
}
</script>
Copy after login
// 孙组件B
<template>
  <div>
    <h1>组件B</h1>
    {{ msgB }}
  </div>
</template>

<script>
export default {
  name: &#39;ChildB&#39;,
  data() {
    return {
      msgB: null
    }
  },
  mounted() {
    this.msgB = this.$attrs
    console.log(&#39;组件B获取的$listeners:&#39;, this.$listeners)
  }
}
</script>
Copy after login

$attrs获取的结果:
How to communicate between Vue components? Several ways of component communication
$listeners获取的结果:

How to communicate between Vue components? Several ways of component communication

如代码和图所示组件A中props声明接收了sex属性,因此组件中&dollar;attrs获取的是父组件中绑定的除去sex属性的值;组件A中使用了v-bind="&dollar;attrs"v-on="$listeners",则组件B获取不仅是组件A中本身绑定的属性和方法还包含组件A获取父组件绑定的属性和方法

3、inheritAttrs

如果父组件传递了很多参数给子组件,而子组件没有用props完全接收,那么没有接收的这些属性作为普通的 HTML attribute 应用在子组件的根元素上
如果你不希望子组件的根元素继承特性,你可以在组件的选项中设置inheritAttrs: false

以上面的组件B为例,当How to communicate between Vue components? Several ways of component communication(inheritAttrs默认为true)
How to communicate between Vue components? Several ways of component communication

当How to communicate between Vue components? Several ways of component communication

// 孙组件B
export default {
  name: 'ChildB',
  inheritAttrs: false,
  data() {
    return {
      msgB: null
    }
  },
  mounted() {
    this.msgB = this.$attrs
    console.log('组件B获取的$listeners:', this.$listeners)
  }
}
Copy after login

How to communicate between Vue components? Several ways of component communication

七、Vuex

1、Vuex概述

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 + 库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。
状态管理包含以下几个部分:
How to communicate between Vue components? Several ways of component communication

  • 状态(State),驱动应用的数据源
  • 视图(View),以声明方式将状态映射到视图;
  • 操作(Actions),响应在视图上的用户输入导致的状态变化

视图发生变化会导致数据源的改变,数据源发生变化则会改变视图,则上面表示是一个“单向数据流”。但是当我们的应用遇到多个组件共享状态时,单向数据流的简洁性很容易被破坏:

  • 多个视图依赖于同一状态。
  • 来自不同视图的行为需要变更同一状态。

因此,为了解决这种问题我们把组件的共享状态抽取出来,以一个全局单例模式管理。在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!

通过定义和隔离状态管理中的各种概念并通过强制规则维持视图和状态间的独立性,我们的代码将会变得更结构化且易维护。
How to communicate between Vue components? Several ways of component communication

2、 Vuex各个模块

1、state:存储应用中需要共享的状态,是Vuex中的唯一数据源。
2、getters:类似Vue中的计算属性computedgetter 的返回值会根据它的依赖被缓存起  来,且只有当它的依赖值发生了改变才会被重新计算。
3、mutations:更改 Vuex 的 store 中的状态(state)的唯一方法,且mutation 必须是同步函数
4、actions:类似于 mutation,提交的是 mutation,而不是直接变更状态;可以包含任意异步操作
5、modules:将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割

3、Vuex举例

// 父组件
<template>
  <div>
    <h1>父组件</h1>
    <hr>
    <childa></childa>
    <hr>
    <childb></childb>
  </div>
</template>

<script>
import ChildA from &#39;./components/ChildA&#39;
import ChildB from &#39;./components/ChildB&#39;

export default {
  name: &#39;parent&#39;,
  components: {
    ChildA,
    ChildB
  }
}
</script>
Copy after login
// 子组件A
<template>
  <div>
    <h1>组件A</h1>
    <p>A获取的值: {{ count }}</p>
    <button>ChildA-add</button>
  </div>
</template>

<script>
export default {
  computed: {
    count() {
      return this.$store.state.count
    }
  },
  methods: {
    // 改变store里count的值
    add(num) {
      this.$store.dispatch(&#39;countAdd&#39;, num)
    }
  }
}
</script>

<style>

</style>
Copy after login
// 子组件B
<template>
  <div>
    <h1>组件B</h1>
    <p>B获取的值: {{ countB }}</p>
    <button>ChildB-add</button>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from &#39;How to communicate between Vue components? Several ways of component communication&#39;
export default {
  computed: {
    ...mapGetters({
      countB: &#39;getCount&#39;
    })
  },
  methods: {
    ...mapMutations([&#39;countAdd&#39;]),
    // 改变store里count的值
    add(num) {
      this.countAdd(num)
    }
  }
}
</script>

<style>

</style>
Copy after login

store.js

import Vue from 'vue'
import Vuex from 'How to communicate between Vue components? Several ways of component communication'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
  },
  getters: {
    getCount: (state) => {
      return state.count
    }
  },
  mutations: {
    countAdd(state, num) {
      state.count += num
    }
  },
  actions: {
    countAdd(context, num) {
      context.commit('countAdd', num)
    }
  },
  modules: {
  }
})
Copy after login

How to communicate between Vue components? Several ways of component communication

八、localStorage/sessionStorage

1、介绍

localStorage:本地存储对象,存储的数据是永久性数据,页面刷新,即使浏览器重启,除非主动删除不然存储的数据会一直存在
sessionStorage:与localStorage相似,但是只有在当前页面下有效,关闭页面或浏览器存储的数据将会清空

localStorage和sessionStorage常用的API:

setItem (key, value) ——  保存数据,以键值对的方式储存信息。
getItem (key) ——  获取数据,将键值传入,即可获取到对应的value值。
removeItem (key) ——  删除单个数据,根据键值移除对应的信息。
clear () ——  删除所有的数据
key (index) —— 获取某个索引的key
Copy after login

2、举例

// 存储
setItem() {
  window.localStorage.setItem('name1', '小明')
  window.sessionStorage.setItem('name2', '小红')
}
Copy after login
// 接收
receive() {
  const name1 = window.localStorage.getItem('name1')
  const name2 = window.sessionStorage.getItem('name2')
  console.log(name1) // 打印结果为:小明
  console.log(name2) // 打印结果为:小红
}
Copy after login

3、setItem()和getItem()使用时的类型转换

localStorage和sessionStorage通过setItem()存储数据会自动转换为String类型,但是通过getItem()其类型并不会转换回来(localStorage和sessionStorage使用方法一样,下面均以localStorage为例)

const num = 1
window.localStorage.setItem('num', num)

const numRec = window.localStorage.getItem('num')
console.log(numRec, typeof(numRec)) // 1 string
Copy after login

因此正确的存储方式应该为:存储之前用JSON.stringify()方法将数据转换成json字符串形式;需要使用数据的时候用JSON.parse()方法将之前存储的字符串转换成json对象

const num = 1
window.localStorage.setItem('num', JSON.stringify(num))
const obj = {
   name: '小红',
   age: 18
 }
window.localStorage.setItem('obj', JSON.stringify(obj))

const numRec = JSON.parse(window.localStorage.getItem('num'))
console.log(numRec, typeof(numRec)) // 1 'number'
const objRec = JSON.parse(window.localStorage.getItem('obj'))
console.log(objRec, typeof(objRec)) // {name: '小红', age: 18} 'object'
Copy after login

注意:localStorage.setItem()和sessionStorage.setItem()不能直接存储对象,必须使用JSON.stringify()JSON.parse()转换实现

总结

以上8种通信方式主要应用在以下三类场景:

  • Communication between parent and child components: The most commonly used communication method is props/&dollar;emit, and single parent-child component communication uses &dollar;parent>/&dollar;children is also more convenient; parent components often use ref to obtain child component instances; you can also use provide/inject, &dollar;attrs/&dollar;listeners, and localStorage/sessionStorage
  • Brother component communication: Simple data transfer can use eventBus&dollar;emit/&dollar;on; complex data can useVuex is more convenient; you can also use localStorage/sessionStorage;
  • Cross-level component communication: Communication methods for nested components such as father, son, and grandson Use provide/inject and &dollar;attrs/&dollar;listeners; the data communicated across level components can be used if it is not complicatedeventBus and localStorage/sessionStorage; if the data is complex, you can use Vuex, but be aware that the data stored in Vuex will disappear when you refresh the interface

End

This article simply records the commonly used component communication methods, and does not introduce its details in depth. If there are any mistakes, please correct me

( Learning video sharing: web front-end development, Basic programming video)

The above is the detailed content of How to communicate between Vue components? Several ways of component communication. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!