我是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 varibale ( this.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.
更多推荐
发布评论