本文介绍了Aurelia JS中的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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{

        }
    });

常用功能

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 varibale( this.sharedAlrdy ).它抛出错误,无法设置属性'sharedAlrdy "(未定义).还有其他方法吗?

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无关.您只是遇到this的典型JavaScript问题.

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

由于您使用的是Aurelia,因此我假设这是ES6代码,并且您具有箭头功能支持.在函数回调中使用箭头函数,捕获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 {

    }
});

有关箭头函数和this的更多详细信息,请查看 MDN .

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

这篇关于Aurelia JS中的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:42