vue组件间传值---上到下 篇

1、父传子 props

父组件:使用propsName="dataName"提供数据,其中dataName可以是一个常量值,也可以是data和computed中的属性,当使用data和computed时
子组件

<template>
  <div class="father">
    <h1>这是father组件</h1>
    <!--  这里传递的是固定值  -->
    <!--    <Son fatherToSon="fatherToSon" />-->
    <!--  这里绑定了data中的值  -->
    <Son :fatherToSon="fatherToSon" />
  </div>
</template>
<script>
  import Son from './Son'
  export default {
    name: "Father",
   components:{
      Son
    },
    data(){
      return {
        fatherToSon:1,
      }
    }
</script>
<style scoped>
</style>

子组件在props里接收父组件传递来的值就可以和data中的值一样使用了
props可以有数组形式和对象形式:
数组形式中每个数组元素是字符串,对应了父组件传来的变量名,也就是上面提到的propsName
使用对象形式时也分为两种情况:
1、props:{propsName1:变量类型,propsName2:变量类型},就是下面的代码写法;
2、对象嵌套

props:{ 
     propsName1:{
       type:变量类型1,
       default:变量默认值1
    }, 
    propsName2:{
       type:变量类型2,
       default:变量默认值2
    }
  }

子组件代码

<template>
  <div class="son">
    <h1>这是son组件</h1>
    <span>来自father的值(fatherToSon):{{fatherToSon}}
    </span>
  </div>
</template>

<script>
  export default {
    name: "Son",
    props: {
      //在这里接收值
      fatherToSon: Number,
    }
</script>

<style scoped>
</style>

2、祖先组件传递到子组件 provide + inject

provide用于在祖先组件提供数据,同样的可以是固定的值,也可以是data或computed中的属性

provide:{
      provideParams:'变量值'
  }

inject用于接收

inject:['provideParams']

这里篇幅原因就不展示GrandPa和Father的代码了,嵌套关系如下:
祖先(Ancestors)->祖父(GrandPa)->父(Father)->子(Son)

祖先组件 Ancestors.vue

<template>
  <div class="ancestors">
    <h1>这是祖先组件</h1>
  </div>
</template>

<script>
  import GrandPa from './GrandPa'
  export default {
    name: "Ancestors",
    provide(){
      return {
        provideFromAncestors:'ancestors'
      }
    },
    components:{
      GrandPa
    },
    data(){
      return{

      }
    }
</script>
<style scoped>
</style>

子组件代码 Son

<template>
  <div class="son">
    <h1>这是son组件</h1>
    <p>来自ancestors的值:{{provideFromAncestors}}</p>
  </div>
</template>

<script>
  export default {
    name: "Son",
    inject:['provideFromAncestors'],
    props: {
      fatherToSon: Number,
    }
  }
</script>

<style scoped>
</style>

3、使用$children和$refs访问子组件

$refs

使用$ref时,需要先在子组件或DOM节点上绑定ref属性,比如son,然后使用this.$refs.sonthis.$refs["son"]访问该节点
父组件

<template>
  <div class="father">
    <h1>这是father组件</h1>
    <button @click="showRef">$ref访问子组件</button>
    <Son ref="son"  />
    <br>
  </div>
</template>

<script>
  import Son from './Son'

  export default {
    name: "Father",
    components: {
      Son
    },
    methods: {
      showRef(){
        console.log(this.$refs.son);
      }
    }
  }
</script>
<style scoped>
</style>

子组件

<template>
  <div class="son">
    <h1>这是son组件</h1>
  </div>
</template>

<script>
  export default {
    name: "Son",
    data(){
      return {
        sonData:1
      }
    },
    methods: {
      sonMethods(){
        
      }
    }
  }
</script>

<style scoped>
</style>

可以看到打印出的$refs['son']的结果时一个vue实例,上面包含了组件上的data、methods等方法和属性

image.png

$children

$children可以访问到当前组件下的所有子组件,这些组件构成一个数组

<template>
  <div class="father">
    <h1>这是father组件</h1>
    <button @click="showRef">$ref访问子组件</button>
    <Son ref="son"  />
    <Son/>
    <br>
  </div>
</template>

<script>
  import Son from './Son'

  export default {
    name: "Father",
    components: {
      Son
    },
    methods: {
      showRef(){
        console.log(this.$refs.son);
        console.log(this.$children);
      }
    }
  }
</script>
<style scoped>
</style>

可以看到返回一个数组,里面两个对象对应了两个Son组件


父组件、祖先组件到子组件传值内容就这些,其中$eventDispatch作为$children的拓展,并不是父-子的过程,而是子-父的过程。

4、使用$children递归向下传递

当某个父组件调用$eventBroadCast时,会递归其每一个子组件,当子组件绑定了对应的event时,会触发对应的函数执行

Vue.prototype.$eventBroadCast = function (event, value) {
    let cpn = this
    const cb = (c) => {
      if (!c.$children){
        return
      }
      c.$children.map((c) => {
        c.$emit(event, value)
          cb(c)
      })
    }
    cb(cpn)
  }

举例:Ancestors向下广播事件ancestorsBroadcast

<template>
  <div class="ancestors">
    <h1>这是祖先组件</h1>
    <button @click="ancestorsBroadcast">祖先组件通知其他子组件</button>
  </div>
</template>

<script>
  import GrandPa from './GrandPa'
  export default {
    name: "Ancestors",
    components:{
      GrandPa
    },
    methods:{
      ancestorsBroadcast(){
        this.$eventBroadCast('ancestorsBroadcast',123)
      }
    }
  }
</script>
<style scoped>
</style>

在Father上进行监听。
这里递归到了son这个子组件上,通过son.$emit,使得father组件对应的函数执行

<template>
  <div class="father">
    <h1>这是father组件</h1>
    <p>来自ancestors的值:{{provideFromAncestors}}</p>
    <button @click="showRef">$ref访问子组件</button>
    <Son @ancestorsBroadcast="ancesterBroadCastToSon"
    />
    <Son/>
    <br>
  </div>
</template>

<script>
  import Son from './Son'

  export default {
    name: "Father",

    components: {
      Son
    },
    },
    methods: {
      cesterBroadCastToSon(value) {
        console.log(`ancester通知到Son,值为${value}`)
      }
    }
  }
</script>

<style scoped>

</style>

同样的,可以在GrandPa上进行监听

<template>
  <div>这是grandPa组件
    <Father @ancestorsBroadcast="ancesterBroadCastToFather" />
  </div>
</template>

<script>
  import Father from "./Father";

  export default {
    name: "GrandPa",
    components: {Father},
    methods: {
      ancesterBroadCastToFather(value) {
        console.log(`ancester通知到Father,值为${value}`)
      }
    }
  }
</script>

<style scoped>

</style>

上级到下级的通信基本就可这些了。事件总线放到下篇再讲吧

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容