首页 > web前端 > js教程 > 正文

VUE2.0组件的传值问题

一个新手
发布: 2017-09-13 10:24:27
原创
1700人浏览过

Vue1.0组件间传递

  使用$on()监听事件;
  使用$emit()在它上面触发事件;
  使用$dispatch()派发事件,事件沿着父链冒泡;
  使用$broadcast()广播事件,事件向下传导给所有的后代

Vue2.0后$dispatch(),$broadcast()被弃用,见https://cn.vuejs.org/v2/guide/migration.html#dispatch-和-broadcast-替换

1.子组件向父组件的传值:

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
登录后复制
<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>
登录后复制

2.父组件向子组件传值

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>
登录后复制

child.vue

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

3.采用eventBus.js传值---兄弟组件间的传值

eventBus.js

import Vue from 'Vue'
export default new Vue()
登录后复制

 App.vue

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

<script>
import FirstChild from './components/FirstChild'
import SecondChild from './components/SecondChild'

export default {
  name: 'app',
  components: {
    FirstChild,
    SecondChild,
  }
}
</script>
登录后复制

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 '../assets/eventBus';
  export default{
    name: 'firstChild',
    data () {
      return {
        message: '你好'
      }
    },
    methods: {
      showMessage () {
       alert(this.message)        bus.$emit('userDefinedEvent', this.message);//传值
      }
    }
  }
</script>
登录后复制

 SecondChild.vue

<template>
    <p id="SecondChild">
        <h1>{{message}}</h1>
    </p>
</template>
<script>
    import bus from '../assets/eventBus';
    export default{
        name:'SecondChild',
        data(){
            return {
                message: ''
            }
        },
        mounted(){
            var self = this;            
            bus.$on('userDefinedEvent',function(message){                
            self.message = message;//接值            
            });
        }
    }
登录后复制

以上就是VUE2.0组件的传值问题的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号