在aurelia js中的回调函数(Callback function in aurelia js)

编程入门 行业动态 更新时间:2024-10-26 12:34:30
在aurelia js中的回调函数(Callback function in aurelia js)

我是aurelia js的新手。 在这里,我需要在aurelia js中进行回调。 这是我试过的代码, file.js

this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry,function(err,result){ if(!err){ console.log("err,result",err,result); this.sharedAlrdy = result; }else{ } });

commonFunctions

get_alrdyShrdUser(docids,callback){ this.httpValueConverter.call_http('sharing/users/list','POST',docids,'test') .then(data => { if(data.meta && data.meta.statusCode == 200) { return callback(null,data.sharedUsers) } }); }

这里一切正常,回调函数也返回值,但我不能赋值给aurelia varibalethis.sharedAlrdy )。它抛出错误, 无法设置未定义属性'sharedAlrdy' 。 那是否有其他方法可以实现?

Am new to aurelia js. Here, i need to do callback in aurelia js . Here is the code i tried, file.js

this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry,function(err,result){ if(!err){ console.log("err,result",err,result); this.sharedAlrdy = result; }else{ } });

commonFunctions

get_alrdyShrdUser(docids,callback){ this.httpValueConverter.call_http('sharing/users/list','POST',docids,'test') .then(data => { if(data.meta && data.meta.statusCode == 200) { return callback(null,data.sharedUsers) } }); }

Here all works fine, callback function also returned value, but i can't assign value to a aurelia varibale(this.sharedAlrdy).It throws error, Cannot set property 'sharedAlrdy' of undefined. Is that any other way to achieve?

最满意答案

这与Aurelia无关。 这只是一个典型的JavaScript问题。

由于您使用的是Aurelia,因此我认为这是ES6代码,并且您有箭头功能支持。 在函数回调中使用箭头函数,并且在捕获时不会遇到问题:

this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry, (err, result) => {
    if (!err) {
        console.log("err, result", err, result);
        this.sharedAlrdy = result;
    } else {

    }
});
 

有关箭头函数和更多细节,请看一下MDN 。

This has nothing to do with Aurelia. You are just having a typical JavaScript problem with this.

Since you are using Aurelia, I assume that is ES6 code and you have arrow function support. Use arrow function in function callbacks and you won't have problems with capturing this:

this.commonFunctions.get_alrdyShrdUser(this.alrdyshardAry, (err, result) => {
    if (!err) {
        console.log("err, result", err, result);
        this.sharedAlrdy = result;
    } else {

    }
});
 

For more details on arrow functions and this, take a look at MDN.

更多推荐

本文发布于:2023-08-06 05:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   函数   js   aurelia   Callback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!