已写的代码:
let data = []
const res = axios.get('http://localhost:8080/area/city?level=1').then(res=>{
data=res.data.body
})
// ...
data.map((city)=>{
if(city.pinyin.slice(0,1).toUpperCase()==item.title){
item.items.push(city);
}
// ...
打印res.data.body后显示:
let data = []
const res = axios.get('http://localhost:8080/area/city?level=1').then(res=>{
data=res.data.body
data.map((city)=>{
if(city.pinyin.slice(0,1).toUpperCase()==item.title){
item.items.push(city);
}
})
// ...
axios 是异步的,数据还没回来就遍历了