美文网首页
vue父子组件

vue父子组件

作者: 2019吖 | 来源:发表于2018-12-05 17:09 被阅读0次

父组件向子组件传参数

<shareBox :index="0" :item="content" :flag="flag" :type="type" :isCenter="true" @closeShare="bgHide"></shareBox>

父组件调用子组件方法

在父组件中引用子组件的标签中使用ref定义组件。

在父组件方法中用$refs找到组件,然后用这个找到要调用的子组件方法(根据子组件方法名称)

<myChild ref="child"></myChild>

<button @click="btn('我是子组件')">确定</button>

methods:{

            btn(txt){

                this.$refs.child.prompts(txt) //通过$refs找到子组件,并找到方法执行

            }

        }

父组件接收子组件传值

在子组件调用的父组件方法中,参数获取

reCollection:function (data) {}


子组件接收父组件参数

props: ['urlObj']

子组件调用父组件方法

在子组件中定义方法,使用$emit调用
reCollection:function () {

    this.myCollect = !this.myCollect;

      this.$emit('reCollection');

      setTimeout(()=>{

          this.close();

     },1500)

}

然后在父组件引用子组件的标签中,加入如下代码

<shareBox :index="0" :item="content" :flag="flag" :type="type" :isCenter="true" @collection="collection"

  @reCollection="messageBoxCofirm" @closeShare="bgHide"></shareBox>

子组件向父组件传值

同样调用$emit方法,传第二个参数

this.$emit('reCollection','这是从子组件传过去的');

相关文章

网友评论

      本文标题:vue父子组件

      本文链接:https://www.haomeiwen.com/subject/rckwcqtx.html