闪电橙工作室
闪电橙工作室
  • 发布:2019-09-28 13:55
  • 更新:2019-09-30 10:24
  • 阅读:4298

uniapp 如何触发子组件的方法,类似wepy的$invoke 那种的解决方案有哪些?

分类:uni-app

子组件的methods部分


           async loadMore(){  
                if (this.noMore){  
                    return false  
                }  
                this.SyncParams.page = this.SyncParams.page + 1  
                await this.getTopics()  
            },  
            async reload(){  
                this.noMore = false  
                this.SyncParams.page = 1  
                await this.getTopics(true)  
            }

主页面部分


       components:{  
            'topic-list':topicList  
        },  
        async onPullDownRefresh(){  
            await this.$refs.topicList.reload()  
            uni.stopPullDownRefresh()  

        },

这样会提示错误

TypeError: Cannot read property 'reload' of undefined
2019-09-28 13:55 负责人:无 分享
已邀请:
youngswelli

youngswelli

用vue的思想的话:不建议你直接去调子组件的方法。
但你可以这样:
子组件

props: {  
  toReload: {  
    type: Boolean,  
    default: false  
  }  
},  
watch: {  
  toReload(toReload) {  
    if (toReload) {  
      this.reload();  
      this.$emit("update", false);  
    }  
  }  
},  
methods: {  
  async loadMore(){    
    if (this.noMore){    
      return false    
    }    
    this.SyncParams.page = this.SyncParams.page + 1    
    await this.getTopics()    
  },    
  async reload(){     
    this.noMore = false    
    this.SyncParams.page = 1    
    await this.getTopics(true)    
  }  
}

父组件

<template>  
  <children-component :toReload:sync="reload"></children-component>  
  <button @click="reload = true">点击重新加载</button>  
</template>  
<script>  
  data() {  
    return: {  
      reload: false  
    }  
  }  
</script>
youngswelli

youngswelli

明确的说:没发现有这种方法。这种方法也不符合vue的思想!有什么问题是不能通过传值和事件回调函数解决的呢?

  • 闪电橙工作室 (作者)

    那我应该如何调用子组件的方法呢

    2019-09-28 20:16

该问题目前已经被锁定, 无法添加新回复