vue组件之间的传递数据

目录

父子

子父

兄弟

爷孙


父子

父组件   father.vue

<template>
    <children :father="father" ref="mychildren"></children>
    <button @click="clickChildrenFunction">调用子组件方法</button>
</template>
<script>
import children from "./components/children";
export default {
    components: {
       children
    },
    data() {
       return {
           father: {isFather:true},
       };
    },
    methods:{
        clickChildrenFunction(){
            this.$refs.mychildren.childrenFunction('调用子组件方法');
        }
    }
}
</script>

子组件    children.vue

props:{
  father:{
    type: Object,
    default() {
       return {};
    },
  }
},
methods:{
  childrenFunction(value){
      console.log(value)
      console.log(this.father)
  }
}

子父

 父组件   father.vue

<template>
    <children @fatherFunction="fatherFunction" ref="mychildren"></children>
</template>
<script>
import children from "./components/children";
export default {
    components: {
       children
    },
    methods:{
        fatherFunction(value){
            console.log("子组件传过来的值",value);
        },
        fatherMethods(value){
            console.log("父页面方法",value);
        },
    }
}
</script>

子组件    children.vue

<template>
  <div>
    <button @click="clickFatherFunction">向父组件传值</button>
    <button @click="clickMethods">调用父组件方法</button>
  </div>
</template>
<script>
export default {
    methods:{
        clickFatherFunction(){
            this.$emit('fatherFunction','子组件向父组件传值');
        },
        clickMethods(){
            this.$parent.fatherMethods('子组件调用父组件方法')
        },
    },
}
</script>

语法糖:.sycn ,双向绑定https://cn.vuejs.org/v2/guide/components-custom-events.html#sync-%E4%BF%AE%E9%A5%B0%E7%AC%A6

父组件 

<template>
  <div>
    <children :title.sync="title"></children>
    <button @click="changeTitle">改变title</button>
  </div>
</template>
<script>
import children from './children'
export default {
  components: {
    child
  },
  data () {
    return {
      title: 'dad'
    }
  },
  methods: {
    changeTitle() {
      this.title = 'dady';
    }
  }
}
</script>

 子组件

<template>
  <div>
    <input :value="title" @input="inputTitle" type="text"/>
  </div>
</template>
<script>
  export default {
    props: {
      title: {
        type: String,
        required: true
      }
    },
    methods: {
      inputTitle(e) {
        this.$emit('update:title', e.target.value);
      }
    }
  }
</script>

父组件和子组件双向绑定

兄弟

eventBus.js

import Vue from 'vue'
export default new Vue()

componentA.vue

<template>
  <div>
    <button @click="tocomponentB">改变title</button>
  </div>
</template>
<script>
import eventBus from './eventBus.js'
export default {
  methods: {
    tocomponentB() {
      eventBus.$emit('componentAprops',"A组件传递的数据")
    }
  }
}
</script>

componentB.vue

<template>
  <div>
    <div>{{componentB}}</div>
    <button @click="tocomponentB">改变title</button>
  </div>
</template>
<script>
import eventBus from './eventBus.js'
export default {
  mounted(){
    this.getcomponentA()
  },
  methods:{
    getcomponentA() {
      eventBus.$on('componentAprops',(val)=>{
          this.componentB=val
       })
    }
  }
}
</script>

爷孙

provide,inject

(ps:provide和inject绑定不是可响应的,上级组件的grandpa改变了,下级组件的this.grandpa的值是不会改变的,然而,如果你传入了一个可监听的对象[对象,方法],那么其对象的属性还是可响应的)

<template>
    <div>
        <grandSun></grandSun>
    </div>
</template>
<script>
export default {
     data() {
		return {
			testvalue:'祖先参数'
		}
	},
     provide:{
        ancestors:this
        grandpa:'爷爷组件参数'
    }
}
</script>

grandSun.vue

<template>
    <div>
        <span>{{grandpa}}</span>
        <span>{{ancestors.testvalue}}</span>
    </div>
</template>
<script>
export default {}
    },
    inject:['grandpa','ancestors'],
}
</script>

$attrs,$listeners

grandpa.vue

<template>
    <div>
        <div>{{fromSunData}}</div>
        <parent :grandpa="grandpa"  @fromSun="fromSun"></parent>
    </div>
</template>
<script>
export default {
    data(){
        return:{
            grandpa:'123'
        }
    },
    methods: {
        fromSun(val) {
            console.log("孙传祖", val);
            this.fromSunData = val;
        },
    },
}
</script>

parent.vue

<template>
    <div>
        <son  v-bind="$attrs" v-on="$listeners"></son>
    </div>
</template>

son.vue

<template>
    <div>
        <span>{{$attrs.grandpa}}</span>
        <button @click="toGrandPa">传爷爷</button>
    </div>
</template>
<script>
export default {
    data(){
        return:{
            sonData:'0000'
        }
    },
    methods: {
        toGrandPa() {
            this.$emit("fromSun", this.sonData);
        },
    },
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值