vue中,axios異步加載數(shù)據(jù),但是有的文件里面需要用到異步拿到的數(shù)據(jù),數(shù)據(jù)還沒拿到,文件已經(jīng)執(zhí)行了,這時(shí)候數(shù)據(jù)就是空,就會(huì)報(bào)錯(cuò),這個(gè)問題怎么解決?具體表現(xiàn):我這邊vue項(xiàng)目是進(jìn)入頁面的時(shí)候會(huì)調(diào)用一個(gè)login方法,然后設(shè)置localStorage,this.login().then(res=>{if(res.code===0){localStorage.setItem(res.data.access_token)}});然后有一個(gè)api.js文件,使用的是axios方法constinstance=axios.create({baseURL:config.BASE_URL,headers:{Authorization:localStorage.getItem('Authorization')||''}});exportdefault{getList(){returninstance.request({url:'123',method:'get'})}}但是因?yàn)閘ogin方法還沒執(zhí)行完api.js文件就已經(jīng)執(zhí)行了,所以localStorage.getItem('Authorization')就是空導(dǎo)致報(bào)錯(cuò),請問這個(gè)問題怎么解決?
異步請求和文件執(zhí)行有沖突,怎么解決?跪求!
DIEA
2019-06-12 09:06:20