Home Web Front-end JS Tutorial Value passing problem of VUE2.0 components

Value passing problem of VUE2.0 components

Sep 13, 2017 am 10:24 AM
components question

##Transfer between Vue1.0 components

Use $on() to listen for events;

Use $emit() to trigger events on it;
Use $dispatch() to dispatch events, which bubble along the parent chain;
Use $broadcast() to broadcast events, and events are propagated downward to all descendants

After Vue2.0, $dispatch() and $broadcast() are deprecated, see https://cn.vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replacement

1. Pass the value from the child component to the parent component:

##Child.vue

<template>
  <p class="child">
    <h1>子组件</h1>
    <button v-on:click="childToParent">想父组件传值</button>
  </p>
</template>
<script>
  export default{
    name: 'child',
    data(){
      return {}
    },
    methods: {
      childToParent(){
        this.$emit("childToParentMsg", "子组件向父组件传值");
      }
    }
  }
</script>parent.vue
Copy after login
<template>
  <p class="parent">
    <h1>父组件</h1>
    <Child v-on:childToParentMsg="showChildToParentMsg" ></Child>
  </p>
</template>
<script>
  import Child from './child/Child.vue'
  export default{
      name:"parent",
    data(){
      return {
      }
    },
    methods: {
      showChildToParentMsg:function(data){
        alert("父组件显示信息:"+data)
      }
    },
    components: {Child}
  }
</script>
Copy after login

2. Parent component passes value to child component

parent.vue

<template>
  <p class="parent">
    <h1>父组件</h1>
    <Child v-bind:parentToChild="parentMsg"></Child>
  </p>
</template>
<script>
  import Child from './child/Child.vue'
  export default{
     name:"parent",
    data(){
      return {
        parentMsg:'父组件向子组件传值'
      }
    },
    components: {Child}
  }
</script>
Copy after login

child.vue

<template>
  <p class="child">
    <h1>子组件</h1>
    <span>子组件显示信息:{{parentToChild}}</span><br>
  </p>
</template>
<script>
  export default{
    name: 'child',
    data(){
      return {}
    },
    props:["parentToChild"]
  }
</script>
Copy after login

3. Use eventBus.js to pass value ---Brother Value transfer between components

##eventBus.js

import Vue from &#39;Vue&#39;
export default new Vue()
Copy after login

App.vue

<template>
  <p id="app">
    <secondChild></secondChild>
    <firstChild></firstChild>
  </p>
</template>

<script>
import FirstChild from &#39;./components/FirstChild&#39;
import SecondChild from &#39;./components/SecondChild&#39;

export default {
  name: &#39;app&#39;,
  components: {
    FirstChild,
    SecondChild,
  }
}
</script>
Copy after login

FirstChild.vue

<template>
  <p class="firstChild">
    <input type="text" placeholder="请输入文字" v-model="message">
    <button @click="showMessage">向组件传值</button>
    <br>
    <span>{{message}}</span>
  </p>
</template>
<script>
  import bus from &#39;../assets/eventBus&#39;;
  export default{
    name: &#39;firstChild&#39;,
    data () {
      return {
        message: &#39;你好&#39;
      }
    },
    methods: {
      showMessage () {
       alert(this.message)        bus.$emit(&#39;userDefinedEvent&#39;, this.message);//传值
      }
    }
  }
</script>
Copy after login

SecondChild.vue

<template>
    <p id="SecondChild">
        <h1>{{message}}</h1>
    </p>
</template>
<script>
    import bus from &#39;../assets/eventBus&#39;;
    export default{
        name:&#39;SecondChild&#39;,
        data(){
            return {
                message: &#39;&#39;
            }
        },
        mounted(){
            var self = this;            
            bus.$on(&#39;userDefinedEvent&#39;,function(message){                
            self.message = message;//接值            
            });
        }
    }
Copy after login

The above is the detailed content of Value passing problem of VUE2.0 components. For more information, please follow other related articles on the PHP Chinese website!

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

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

Clustering effect evaluation problem in clustering algorithm Clustering effect evaluation problem in clustering algorithm Oct 10, 2023 pm 01:12 PM

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Teach you how to diagnose common iPhone problems Teach you how to diagnose common iPhone problems Dec 03, 2023 am 08:15 AM

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

Label acquisition problem in weakly supervised learning Label acquisition problem in weakly supervised learning Oct 08, 2023 am 09:18 AM

The label acquisition problem in weakly supervised learning requires specific code examples. Introduction: Weakly supervised learning is a machine learning method that uses weak labels for training. Different from traditional supervised learning, weakly supervised learning only needs to use fewer labels to train the model, rather than each sample needs to have an accurate label. However, in weakly supervised learning, how to accurately obtain useful information from weak labels is a key issue. This article will introduce the label acquisition problem in weakly supervised learning and give specific code examples. Introduction to the label acquisition problem in weakly supervised learning:

See all articles