大話西游666
2019-03-14 18:19:55
vue中遇到的小問(wèn)題,this.XXX不能直接拿到需要等待一會(huì)才能拿到是為什么???我現(xiàn)在是在調(diào)用兩個(gè)接口,比如是A和B,等A結(jié)束之后我才可以調(diào)用B接口,(因?yàn)锳接口返回?cái)?shù)據(jù)我拿到B接口要用到)我代碼是這樣的 let promise = new Promise(function(resolve, reject) { //獲取Access Token axios.get("api/token?grant_type=client_credential&appid=appid&secret=secret") .then(function(response) { _this.access_token=response.data.access_token; console.log(_this.access_token); }) .catch(function(error) { // error console.log(error); }); resolve(); }); promise.then(function() { //獲取jsapi_ticket console.log(_this.access_token); axios.get("api/ticket/getticket?access_token="+_this.access_token+"&type=jsapi") .then(function(response) { //數(shù)據(jù) success console.log(response); }) .catch(function(error) { // error console.log(error); }); }); A接口完成之后拋出resolve B接口訪問(wèn),但是我的_this.access_token是沒(méi)有值得,必須在里面加一個(gè)一次性定時(shí)器,延遲一下才可以,請(qǐng)問(wèn)是為什么呢?是空的,加個(gè)延遲就可以了,為什么呢? setTimeout(()=>{ console.log(_this.access_token); },600)
4 回答

小唯快跑啊
TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
你好,試試axios.get().then(function(response){
resolve(); //這個(gè)放在請(qǐng)求完成的函數(shù)中調(diào)用
})

幕布斯6054654
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
添加回答
舉報(bào)
0/150
提交
取消