search
HomeWeb Front-endJS TutorialVue component communication (detailed tutorial)
Vue component communication (detailed tutorial)Jun 07, 2018 am 11:52 AM
vuevue component communicationcomponents

This article mainly introduces you to four methods of Vue component communication, namely parent-child component communication, non-parent-child component eventBus communication, using localStorage or sessionStorage, and using Vuex. Friends in need can refer to it. Let’s study together below.

Preface

As we all know, vue is a mvvm framework. One of its biggest differences compared to jquery is that between components of communication. The focus of this article is to sort out the first two, parent-child component communication and eventBus communication. I think the instructions in the Vue document are still a bit simple, and I didn't understand them the first time.

  • Communication of parent-child components

  • EventBus communication of non-parent-child components

  • Use local cache to implement Component communication

  • Vuex communication

##The first communication method: parent-child component communication

Parent component passes data to child component

The parent component needs to do 4 things in total

1.

import son from './son .js' Introduce the sub-component son

2. Register all sub-component names in

components: {"son"}

3. In the parent component template applies sub-component,

4. If you need to pass data to the sub-component, write

 // 1.引入子组件 
 import counter from './counter'
 import son from './son'
// 2.在ccmponents里注册子组件
 components : {
 counter,
 son
 },
// 3.在template里使用子组件
 <son></son>
 // 4.如果需要传递数据,也是在templete里写
 <counter :num="number"></counter>

The sub-component only needs to do one thing

1. Use props to accept data, and you can use the data directly

2. The data received by the sub-component cannot be modified. If you really need to modify it, you can use calculated properties, or assign the data to a variable in the child component data

 // 1.用Props接受数据
 props: [
  &#39;num&#39;
  ],
// 2.如果需要修改得到的数据,可以这样写
 props: [
  &#39;num&#39;
 ],
 data () {
 return {
  number : this.num
 }
 },

The child component passes data to the parent component

Parent The component needs to do a total of 2 things

Define the event in the template

Write a function in methods to monitor the event triggering of the sub-component

// 1. 在templete里应用子组件时,定义事件changeNumber
 <counter :num="number"
   @changeNumber="changeNumber"
 >
 </counter>
// 2. 用changeNumber监听事件是否触发
 methods: {
  changeNumber(e){
  console.log(&#39;子组件emit了&#39;,e);
  this.number = e
  },
 }

The sub-component needs a total of 1 thing

After the data changes, use $emit to trigger it

// 1. 子组件在数据变化后,用$emit触发即可,第二个参数可以传递参数
 methods: {
  increment(){
   this.number++
   this.$emit(&#39;changeNumber&#39;, this.number)
  },
 }

Second communication method: eventBus##eventBus This communication method is aimed at communication between non-parent and child components. Its principle is still through event triggering and monitoring.

But because they are non-parent-child components, they need an intermediate component to connect.

I use it by defining a component that can be accessed by all components on the root component, which is the #app component. The specific usage is as follows

Using eventBus to pass data, we have a total of Three things need to be done

1. Add the Bus attribute to the app component (so that all components can access it through

this.$root.Bus

, and there is no need to introduce any files) 2. In component 1,

this.$root.Bus.$emit

triggers the event 3. In component 2,

this.$root. Bus.$on

Listening events<pre class='brush:php;toolbar:false;'>// 1.在main.js里给app组件,添加bus属性 import Vue from &amp;#39;vue&amp;#39; new Vue({ el: &amp;#39;#app&amp;#39;, components: { App }, template: &amp;#39;&lt;App/&gt;&amp;#39;, data(){ return { Bus : new Vue() } } })</pre><pre class='brush:php;toolbar:false;'>// 2.在组件1里,触发emit increment(){ this.number++ this.$root.Bus.$emit(&amp;#39;eventName&amp;#39;, this.number) },</pre><pre class='brush:php;toolbar:false;'>// 3.在组件2里,监听事件,接受数据 mounted(){ this.$root.Bus.$on(&amp;#39;eventName&amp;#39;, value =&gt; { this.number = value console.log(&amp;#39;busEvent&amp;#39;); }) },</pre>

The third communication method: using localStorage or sessionStorageThis communication is relatively simple, The disadvantage is that the data and status are chaotic and not easy to maintain.

Pass

window.localStorage.getItem(key)

Get dataPass

window.localStorage.setItem(key,value)

Store data

Note: Use JSON.parse() / JSON.stringify() for data format conversion.

The fourth communication method: using VuexVuex is more complicated, you can write a separate article

The above is me I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Use SpringMVC to solve vue cross-domain requests

New features of webpack 4.0.0-beta.0 version (details Tutorial)

How to implement value passing and communication in vue2.0 components

The above is the detailed content of Vue component communication (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Vue常见面试题汇总(附答案解析)Vue常见面试题汇总(附答案解析)Apr 08, 2021 pm 07:54 PM

本篇文章给大家分享一些Vue面试题(附答案解析)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

5 款适合国内使用的 Vue 移动端 UI 组件库5 款适合国内使用的 Vue 移动端 UI 组件库May 05, 2022 pm 09:11 PM

本篇文章给大家分享5 款适合国内使用的 Vue 移动端 UI 组件库,希望对大家有所帮助!

vue中props可以传递函数吗vue中props可以传递函数吗Jun 16, 2022 am 10:39 AM

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

手把手带你利用vue3.x绘制流程图手把手带你利用vue3.x绘制流程图Jun 08, 2022 am 11:57 AM

利用vue3.x怎么绘制流程图?下面本篇文章给大家分享基于 vue3.x 的流程图绘制方法,希望对大家有所帮助!

聊聊vue指令中的修饰符,常用事件修饰符总结聊聊vue指令中的修饰符,常用事件修饰符总结May 09, 2022 am 11:07 AM

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?React和Vue项目的解决方法浅析如何覆盖组件库样式?React和Vue项目的解决方法浅析May 16, 2022 am 11:15 AM

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

通过9个Vue3 组件库,看看聊前端的流行趋势!通过9个Vue3 组件库,看看聊前端的流行趋势!May 07, 2022 am 11:31 AM

本篇文章给大家分享9个开源的 Vue3 组件库,通过它们聊聊发现的前端的流行趋势,希望对大家有所帮助!

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool