首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lwc -如何从lwc回调中调用函数

lwc -如何从lwc回调中调用函数
EN

Stack Overflow用户
提问于 2022-11-29 05:42:40
回答 1查看 31关注 0票数 1

未能将此挑战与文档协调:/希望有人能向我指出为什么当这个lwc呈现(成功)并通过其empApi订阅接收一个事件时,它抛出一个'handleGoNext未定义‘运行时错误。我知道这个函数是不可见的,但是我不能构造这样的东西,这样就可以成功地进行函数调用。调用this.handleGoNext()也不起作用。任何指点都将不胜感激!

代码语言:javascript
复制
handleGoNext(){
    // *** this is the logic Im hoping to call ***
};


// Initializes the component
connectedCallback() {
   if(this.subscription = {}){ 
        
        // Callback invoked whenever a new event message is received
        
        const messageCallback = function (response) {
            handleGoNext(); // *** THIS IS THE CALL THAT BREAKS THINGS ***
        };
        
        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then((response) => {
            // Response contains the subscription information on subscribe call
            console.log(
                'Subscription request sent to: ',
                JSON.stringify(response.channel)
            );
            this.subscription = response;
        });
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-29 07:05:59

当你做subscribe(this.channelName, -1, this.handleGoNext)时会发生什么

这是我在复活节前后的功能,最近没有检查它是否仍然有效。

代码语言:javascript
复制
isSubscribed = false;
subscription = {};
replayId;

@track data = [];

subscribe(id) {
        const handleMessage = (response) => {
            let val = Object.assign({}, response.data.event, response.data.payload);
            this.replayId = Math.max(this.replayId ?? 0, val.replayId);

            /*  do not use this.data.push(val);
                it doesn't play well with datatable. Use the spread operator or JSON.parse/JSON.stringify trick.
                Plus we want new rows to be added on top of the list.`
            */
            this.data = [val, ...this.data];
        };

        subscribe(this.channel, id, handleMessage).then((response) => {
            this.subscription = response;
            this.isSubscribed = true;
        });
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74609593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档