第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Vue.js父子組件和非父子組件間的傳值通信

標(biāo)簽:
Vue.js

[toc]

父子组件的传值通信

父组件向子组件传值

  • 父组件:

<template>
    <child :message="parentMessage"></child>
</template>

data () {    return {
        parentMessage: "this is a message from parent"
    }
}
  • 子组件:

<template>
    <p>{{message}}</p>
</template>/* 一般形式 */data () {
    props: ["message"]
}/* 指定接收类型 */data () {
    props: {
        message: {
            type: String, //接收类型
            default: "this is the default value" // 默认值
        }
    }
}

子组件向父组件传值

Note 子组件不能直接更改父组件中的内容,因此可以通过子组件触发事件来传值给父组件。

  • 父组件:

<template>
    <div class="root">
        /* 自动监听子组件注册的 getChildValue 事件*/        <child @getChildValue="receive"></child>
        
        <p>{{valueFromChild}}</p>
    </div></template>data () {
    return {
        valueFromChild: "defaultValue"
    }
},
methods: {
    receive (valueFromChild) {
        this.valueFromChild = valueFromChild
    }
}
  • 子组件:

<template>
    <button @click="sendValueToParent">click to send value to parent</button>
</template>

data () {    return {
        childValue: 'this is child Value'
    }
},
methods: {
    sendValueToParent () {        /* 将 childValue 传递给父组件 */
        this.$emit('getChildValue', this.childValue)
    }
}

非父子组件之间的传值通信

  1. 创建 eventBus.js

import Vue from 'vue'var bus = new Vue()export default bus
  1. 组件 A

<template>
    <div class="root">
        <button @click="sendMessageToB">click here to send a message to B</button>
    </div></template>import eventBus from '.../eventBus.js'

data () {
    return {
        message: "message from A"
    }
},
methods: {
    sendMessageToB () {
        eventBus.$emit('transfer', this.message); 
    }
}
  1. 组件 B

<template>
    <div class="root">{{messageFromA}}</div>
</template>

import eventBus from '.../eventBus.js'data () {    return {
        messageFromA: "defaultValue"
    }
},created () {
    eventBus.$on('transfer', function (message) {
        this.messageFromA = message
    })
}



作者:沙海飞鱼
链接:https://www.jianshu.com/p/75ff1773ce41


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消