1 回答

TA贡献1834条经验 获得超8个赞
我相信这个nuxtServerInit动作被恰当地调用了。我敢打赌,它message的值始终为未定义,因为 Axios.get("http://localhost:5000/api/home")返回的 promise 没有属性data。您必须先等待结果,然后获取它的数据。
// note extra parentheses
const message = await (Axios.get("http://localhost:5000/api/home")).data;
此外,您可能希望将 axios 调用包装在 try-catch 中,否则如果调用失败,您将获得带有堆栈跟踪的默认 Nuxt 错误页面。
async nuxtServerInit ({ commit }: any) {
try {
const message = (await Axios.get('http://localhost:5000/api/home')).data;
console.log(message);
commit('setMessage', message); // put name of mutation as parameter + payload
} catch (error) {
// you could redirect to custom error page for instance
console.error(error);
}
}
添加回答
举报