vue.js - Vue Eventbus : handler. apply 不是一个函数

标签 vue.js vue-component event-bus

当另一个组件发生更改时,我想重新加载其中一个组件(例如我使用 axios 发送 put)

我尝试使用 eventbus 但出现此错误:

handler.apply is not a function

在我想要触发的组件中:

EventBus.$emit('compose-reload', Math.random()*100);

我想在哪里触发:

<template>
    <div class="">

        <div class="">
            <div class="columns">
                <div class="column is-one-fifth">
                    <div class="box">
                        <Menu></Menu>
                    </div>
                </div>
                <div class="column is-four-fifth">
                    <div class="box">
                        <router-view :key="key"></router-view>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>
<script>
    import Menu from './includes/Menu'

    import EventBus from '../../../event-bus';

    export default {

        components: {
            Menu,
        },
        data() {
            return {
                key: null
            }
        },
        mounted(){
            EventBus.$on('compose-reload', this.key);
        },
        created(){
            this.key = Math.random()*100
        }
    }
</script>

最佳答案

EventBus.$on 期望处理函数作为第二个参数,但传入了变量 this.key,因此出现错误。

你应该改变这个:

mounted(){
   EventBus.$on('compose-reload', this.key);
}

对此:

mounted(){
   EventBus.$on('compose-reload', key => {
     this.key = key;
   });
}

关于vue.js - Vue Eventbus : handler. apply 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363201/

相关文章:

javascript - JSON 对象数组到单个字符串

javascript - Karma Browserify bundle 错误

guava - 为什么 Guava 会发出错误 "missing event handler for an > annotated method"?

android - otto eventbus for android 在发布版本中表现不同

android - EventBus - 订阅者类及其父类(super class)没有带有 @subscribe 注释的公共(public)方法

html - 在 Vue.js 中正确格式化 <pre> 和 <code> block

vue.js - 使用 VueJS 2 重用模态

vue.js - Vue 组合 api - 将变量传递给我的组件 data() 选项?

javascript - vue组件外部的访问方法

vue.js - 我该如何解决 "Interpolation inside attributes has been removed. Use v-bind or the colon shorthand"? Vue.js 2