uni-app 在父组件中调用子组件的方法

 

用途与示例
1.父组件可以使用 props 把数据传给子组件。
1.子组件可以使用 $emit 触发父组件的自定义事件。

子组件:

<template>
<form>
<button :click="search" value="搜索"/>
</form>
</template>
<script>
methods: { search(e) { this.$emit('search',“给父组件传的参数”); } }
</script>

父组件:

<template>

<bottomNavBar v-on:search="search"></bottomNavBar>   

</template>

<script>
export default
{
components:{bottomNavBar}
methods:
{
  search(getKeyWord)
            {
                this.keyWord=getKeyWord;/*得到子组件search传过来的参数*/
                
            },
}
}
</script>

相关文档:

vm.$emit( eventName, […args] )

触发当前实例上的事件。附加参数都会传给监听器回调。

https://cn.vuejs.org/v2/api/#vm-emit

https://cn.vuejs.org/v2/guide/components-custom-events.html

 

posted @ 2021-04-26 16:32  fogwu  阅读(6069)  评论(0)    收藏  举报