简体   繁体   English

VueJs:如何使用 $emit(optionalPayload) 和 $on 将“计算函数”值从子组件传递到父组件?

[英]VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on?

I am new to VueJs and I am doubtful about passing the optional payload Could anyone please tell me how to pass the value returned by a computed function in the child component to the parent component using the optional payload.我是 VueJs 的新手,我对传递可选有效负载持怀疑态度 谁能告诉我如何使用可选有效负载将子组件中的计算函数返回的值传递给父组件。

I want to implement a separate independent search component which returns the search results to all other components.我想实现一个单独的独立搜索组件,它将搜索结果返回给所有其他组件。 The computed function looks like this:计算函数如下所示:

get computedSports () {
    if (!this.searchModel || this.searchModel.length === 0)
       return this.sports
    else
       return this.fuseSearch.search(this.searchModel)
}

This is how I am trying to pass the value returned by computed function to its parent component in the child template:这就是我试图将计算函数返回的值传递给子模板中的父组件的方式:

@input="$bus.$emit('computed-sports', computedSports)"

In the parent component, this is how I am trying to access the value of the child's computed function: v-on:computed-sports=""在父组件中,这就是我尝试访问子计算函数值的方式: v-on:computed-sports=""
I am not quite sure how to access the value here.我不太确定如何在此处访问该值。 Could anyone help me out in this?谁能帮我解决这个问题?

Thanks!谢谢!

The argument to a v-on should be a method or inline function that takes the payload as an argument. v-on的参数应该是一个将有效负载作为参数的方法或内联函数。 For example例如

v-on:computed-sports="handleComputedSports"

and your method might be defined并且您的方法可能已定义

handleComputedSports(theValue) {
  console.log("The value is", theValue);
}

There's an example in this section of the documentation . 文档的本节中有一个示例。 Your emit is fine.你的发射很好。 The fact that the value comes from a computed makes no difference to anything.值来自计算的事实对任何事情都没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM