Vue:“TypeError:handler.apply 不是函数”-($emit 在 jQuery 侦听器回调中)

问题描述 投票:0回答:1

我已经在 vue 组件内的元素上初始化了 jQuery 插件。在初始化函数中,我有一个“

$(document).ready(function()
”,这意味着“
this
”在内部不起作用。所以我使用 var
that = this

但是,还注册了一个侦听器,并且在该侦听器内我需要发出一个值:

initialize() {

     var that = this;

    $(document).ready(function() {

       let editor = that.$refs.myplugin

      //initialize   
       var $editor  = $(editor ).myplugin({

            height: 300
       })

       //listener callback
       $editor.on('myplugin.change', (e) => {

           //results in error
           that.$emit('input', 'test message');

        }) 
    }) 
}

该值确实被发出,但每次都会出现错误“v-on handler 中的错误:“

TypeError: handler.apply is not a function
”。如果我注释掉 $emit,则不会有错误。

所以

that
在回调内部工作(我可以调用测试函数),但发出。

那么为什么会显示错误以及如何在侦听器回调中使用

$emit
正确性?

更新: 尝试将箭头函数改为普通函数,但还是同样的错误。

vue.js scope event-listener
1个回答
0
投票

我最近在一个用 Vue.js v2 编写的遗留项目中遇到了这个错误。 就我而言,问题是因为当组件中发出的事件触发时,我试图运行计算函数。 请检查您的事件触发时运行的内容。

© www.soinside.com 2019 - 2024. All rights reserved.