vue 父子組件通信

父組件 ------------------------------------------------------

<template>

? ? <div class="home">

? ? ? ? <img alt="Vue logo" src="../assets/logo.png">

? ? ? ? <!--? :fatherMsg ?在子組件標(biāo)簽上通過(guò) :屬性名 傳遞給子組件 -->

? ? ? ? <!-- @toFatherManyArgs/@toFatherOneArg 是子組件 $emit傳過(guò)來(lái)的第一個(gè)參數(shù) -->

? ? ? ? <!-- 當(dāng)只有一個(gè)參數(shù)時(shí), 接收參數(shù)的關(guān)鍵詞是: $event -->

? ? ? ? <!-- 當(dāng)有多個(gè)參數(shù)時(shí), 接收參數(shù)的關(guān)鍵詞是: arguments -->

? ? ? ? <ChildComponent :fatherMsg='toChild'?

? ? ? ? ? ? ? ? ? ?@toFatherManyArgs="toFatherManyArgs(arguments)"

? ? ? ? ? ? ? ? ? ?@toFatherOneArg="toFatherOneArg($event)">

? ? ? ? </ChildComponent>


? ? ? ? {{receiveChild1}} {{receiveChild2}} {{receiveChild3}}

? ? </div>

</template>

<script>

import ChildComponent from '../components/Child.vue'

export default {

? ? name: 'HomeView',

? ? data() {

? ? ? ? return {

? ? ? ? ? ? toChild: "world !",

? ? ? ? ? ? receiveChild1: "",

? ? ? ? ? ? receiveChild2: "",

? ? ? ? ? ? receiveChild3: "",

? ? ? ? }

? ? },

? ? components: {

? ? ? ? ChildComponent

? ? },

? ? methods: {

? ? ? ? toFatherManyArgs(args) {

? ? ? ? ? ? this.receiveChild1 = args[0];

? ? ? ? ? ? this.receiveChild2 = args[1];

? ? ? ? },

? ? ? ? toFatherOneArg(arg) {

? ? ? ? ? ? this.receiveChild3 = arg;

? ? ? ? }

? ? }

}

</script>


子組件 ------------------------------------------------------

<template>

? ? <div id="child-box">

? ? ? ? {{message}}

? ? ? ? {{fatherMsg}}

? ? </div>

</template>

<script>

export default {

? ? name: 'ChildView',

? ? // 通過(guò) props 接收父組件傳過(guò)來(lái)的字段

? ? props: ["fatherMsg"],

? ? data() {

? ? ? ? return {

? ? ? ? ? ? message: 'hello',

? ? ? ? ? ? say: 'nice to meet you',

? ? ? ? }

? ? },

? ? created() {

? ? ? ? this.$emit('toFatherManyArgs', this.say, "kangkang")

? ? ? ? this.$emit('toFatherOneArg', "meimei")

? ? },

}

</script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 組件是 vue.js 最強(qiáng)大的功能之一,而組件實(shí)例的作用域是相互獨(dú)立的,這就意味著不同組件之間的數(shù)據(jù)無(wú)法相互引用。...
    風(fēng)語(yǔ)者Alex閱讀 297評(píng)論 0 0
  • 父親<template>//chriden對(duì)應(yīng)兒子的文件名字 oldInfo對(duì)應(yīng)你需要傳給子組件的數(shù)據(jù) cl...
    heler閱讀 388評(píng)論 0 1
  • 一 父組件向子組件通過(guò)props傳遞數(shù)據(jù) 在組件中,使用選項(xiàng)props來(lái)聲明需要從父級(jí)接收到的數(shù)據(jù)。props的值...
    名字是亂打的閱讀 249評(píng)論 0 1
  • 1.子組件的 props 屬性通過(guò)子組件的 props 屬性,可以將父組件上的數(shù)據(jù)傳入子組件,如下例中的 deli...
    極客傳閱讀 199評(píng)論 0 0
  • vue設(shè)計(jì)模式是數(shù)據(jù)流在組件之間是單向流動(dòng),組件內(nèi)部是數(shù)據(jù)雙向綁定, 父組件一般會(huì)通過(guò)props綁定數(shù)據(jù)傳遞給子組...
    杰出嚕閱讀 293評(píng)論 0 0

友情鏈接更多精彩內(nèi)容