The problem is here
for (const item of response.data) {
axios.get('/api/2', {
headers : {
'Access-Control-Allow-Origin' : '*',
'session' : window.session_id
}
}).then((response) => {
// This finishes later than
// this.list = response.data occurs
item.subscribers = response.data.filter(o => { return o.status > 0 }).length;
});
};
this.list = response.data;
You assign this.list
which triggers Vue to re-render. Only later you assign subscribers to each individual item which Vue cannot detect. There are plenty of ways how to properly wait for the response. Because you are basically need everything to resolve in a synchronous way, I'd suggest to use async/await syntax for readability. I haven't tested the code, there might be a typo. But it gives you an idea I hope.
async function test() {
try {
const response = await axios.get('/api/1', {
headers: {
'Access-Control-Allow-Origin': '*',
session: window.session_id,
},
});
await Promise.all(response.data.map(async (item) => {
const subscribers = await axios.get('/api/2', {
headers: {
'Access-Control-Allow-Origin': '*',
session: window.session_id,
},
});
item.subscribers = subscribers.filter((o) => o.status > 0).length;
}));
this.list = response.data;
} catch (error) {
console.log(error);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…