子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp

(一) popsDowm

  三种方法获取父组件数据:被动获得(1);主动获取(2)。

  1.被动获得:

    父组件:v-bind: 绑定变量参数和方法参数;子组件:props 接收参数。可以在模板中直接使用也可以 this.参数 获得

v-bind:name="yourName"
props:['name']

template: {{name}}

js: this.name

v-bind:fatherMeth="regMeth"
props:{fatherMeth: Function} js: this.fatherMeth()
 


  2.主动获取:

    ①this.$parent.变量和方法

       this.$parent.yourName;this.$parent.fatherTest().

        如果获取不到,在父组件将 this 传递给子组件::father="this"

       this.father.youerName;this.father.fatherTest().

    ② 发送 emit 事件

         this.$emit('fatherOnEmite',this.childVariate)

(二) eventUp:

  两种方法: 在父组件设置 ref 属性;监听 emit (订阅)。

  父组件:ref="child1" 

    this.$refs.child1.desc;this.$refs.child1.childTest()

  父组件:v-on:fatherOnEmite="onEmite"

  注意:由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。updated 不能保证全部组件都加载用 this.nextTick

代码github:https://github.com/dopocheng/alone-part

父组件



 

  子组件:


 

你可能感兴趣的:(子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp)