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

Value passing problem of VUE2.0 components

一个新手
Release: 2017-09-13 10:24:27
Original
1362 people have browsed it

##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!

Related labels:
source:php.cn
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