vue组件之间的传值

slot标签:
想向封装好结构的组件中插入内容,需要借助<slot></slot>
在组件之中进行关联:如
模板中:<slot name='txt'></slot>
组件调用中:<p slot='txt'></p>
注:如果只有slot上面每一定义name属性,则只能有一个slot

<div class='box'>
    <com>
        <p slot='txt'></p>  
    </com>
</div>  
<template id="c">
    <div>
        <slot name="txt"></slot>    
    </div>
</template>

Vue.component('com',{
    template:"#c"
})

父组件向子组件传值:props
父组件:

<template>
    <child :parent-msg='a'></child>
</template>

子组件:

child:{
    template:‘#chi’
    props:['parentMsg']
}

子组件向父组件的传值:
vue只运行数据的单选传递,在子组件向父组件的传值中,需要事件触发
子组件:

<template>
    <div @click="up"></div>
</template> 

methods:{
 up(){
    this.$emit('fn','msg') // 主动触发fn方法,msg是需要传递的数据 
 }
}

父组件:

<div>
    <child @fn="getval"></child>
</div>

methods:{
    getval(msg){ // msg接收到的数据
        this.msg=msg
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值