ifFollowed:function() {
var _this = this;
uni.request({
url: this.apiServer + '/follow/one',
method: 'POST',
header: { 'content-type': 'application/x-www-form-urlencoded' },
data: {
fromUId: uni.getStorageSync('login_key').userId,
toUId: 7
},
success: (res) => {
if(res.data.data!=null){
return false;
}else{
return true;
}
}
});
},如代码所示。我想判断data的值然后返回到函数。这样应该是错的。百度说要异步。但是我不会,有大神帮忙改一下吗

解决方案 »

  1.   

    用回调函数来处理返回结果ifFollowed:function(callback){
    ...
    if(res.data.data!=null){
    callback(false);
    }else{
    callback(true);
    }
    }obj.ifFollowed(function(result)){
    console.log(result);
    //后续操作
    });
      

  2.   

    <button v-if="ifFollowed(followed.fromUId)" class="followed-btn">关注</button>
    ifFollowed:function(aId) {
    var _this = this;
    uni.request({
    url: this.apiServer + '/follow/one',
    method: 'POST',
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    data: {
    fromUId: uni.getStorageSync('login_key').userId,
    toUId: aId
    },
    success: (res) => {
    if(res.data.data!=null){
    return false;
    }else{
    return true;
    }
    }
    });
    },
    完整是这样的,请问我改如何在上面调用啊。我想用这个值判断button是否显示。昨晚太晚了。忘记发全了
      

  3.   


    button上设置 v-if="showFollow"在mounted里调用this.isFollowed(followed.fromUId)if(res.data.data!=null){
            _this.showFollow = false;
        }else{
            _this.showFollow = true;
        }
      

  4.   

    还是不懂,我的fromUId是循环的,不过谢谢大神了。这个功能我就不写了。太麻烦了