vue2.0组件间传值的方法汇总

1、组件间的传值


1.1 父组件向子组件传值

子组件自定义一个属性


父组件通过自定义属性绑定值


子组件调用自定义属性来拿到值


1.1 完)

1.2 子组件向父组件传值

子组件自定义一个事件


父组件通过自定义事件绑定一个方法来接收值




1.3 兄弟组件间传值

新建一个js文件作为兄弟组件间传值的中转站(new 一个vue)


组件1把值先传到bus


组件2从bus接收值



【完整代码】

parent.vue(父组件)

import Children1 from './children1'

  import Children2 from './children2'

    export default {

        name: "parent",

      components:{

        Children1,

        Children2

      },

      data(){

          return {

            toChildren:'',

            getChildren:''

          }

      },

      methods:{

          getChildrenData(data){

            this.getChildren = data;

          }

      }

    }  body{

    background: #eee;

  }

  .container{

    width: 100%;

  }

  .header{

    width: 100%;

  }

  .main{

    width: 985px;

    margin: 50px auto;

  }

  .children1,.children2{

    width: 40%;

    height: 300px;

    margin: 0 20px;

    display: inline-block;

    background: #909399;

    color: #fff;

  }

  .getData{

    color: darkred;

    height: 50px;

  }

children1.vue 子组件1

import bus from '../eventBus/bus'

    export default {

        name: "children",

      props:['getData'],    //子组件自定义一个属性

      data(){

          return {

            toBrother:''

          }

      },

      methods:{

        dataToBrother(){

          bus.$emit('toBrother',this.toBrother)

        }

      }

    }  h1{

    height: 50px;

    color: darkred;

  }

children2.vue 子组件2

import bus from '../eventBus/bus'

    export default {

        name: "children2",

      data(){

          return {

            toParentData:'',

            dataFromBrother:''

          }

      },

      created(){

          bus.$on('toBrother',(data)=>{

            this.dataFromBrother = data;

          })

      },

      methods:{

        dataToParent(){

          this.$emit('toParent',this.toParentData)

        }

      }

    }  h1{

    height: 50px;

    color: darkred;

  }

bus.js

import Vue from 'vue'

export default new Vue

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2.0 中父子组件传值可以通过属性(prop)和事件(event)实现。 1. 属性(prop)传递值 父组件通过在子组件上定义prop属性,指定要传递的值类型和名字,然后将值传递给子组件的prop属性。子组件通过props接收到父组件传递的值,可以直接使用。 例如:父组件中定义一个名为message的prop属性,值为字符串类型,在模板中通过子组件标签向子组件传递值。 ``` // 父组件 <template> <div> <child-component :message="hello"></child-component> </div> </template> <script> import ChildComponent from './child-component.vue' export default { components: { ChildComponent }, data () { return { hello: 'hello, world' } } } </script> // 子组件 <template> <div>{{ message }}</div> </template> <script> export default { props: { message: { type: String, require: true } } } </script> ``` 2. 事件(event)传递值 父组件通过定义一个事件,在子组件中触发该事件并传递需要传递的值。子组件在父组件中使用子组件自定义的事件,并获取到传递的值。 例如:子组件定义一个自定义事件,触发时传递一个字符串类型的值。父组件中使用子组件,同时监听子组件的自定义事件,在事件回调函数中获取到子组件传递的值。 ``` // 子组件 <template> <button @click="changeMessage()">改变消息</button> </template> <script> export default { methods: { changeMessage () { this.$emit('change', '新消息') } } } </script> // 父组件 <template> <div> <child-component @change="handleChange"></child-component> <div>{{ message }}</div> </div> </template> <script> import ChildComponent from './child-component.vue' export default { components: { ChildComponent }, data () { return { message: '旧消息' } }, methods: { handleChange (newValue) { this.message = newValue } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值