我想在asyncData中访问我的Vuex数据,但是我不能访问,而且我似乎不能在asyncData中使用axios模块。
我试了很多次。
pages/index.vue
export default {
asyncData() {
//in my project there's a lot more code here but i think this is enough
let id = this.$store.state.id //i know i should prob use getter but no
//here i want to do an axios req with this var "id" but axios doesnt work
return axios.get(`url${id}`)
.then(...) //not gonna write it out here
}
}store/index.js
export const state = () => ({
id: "#5nkI12_fSDAol/_Qa?f"
})我希望它从Vuex获得ID,然后执行Axios req。整个应用程序都不起作用。
发布于 2019-07-28 15:11:47
您不能在this中使用asyncData。函数asyncData作为param传递一个上下文对象,它基本上表示this关键字。
首先,您需要指定要从context对象检索什么,然后可以使用它。
在你的例子中,做这样的事情:
export default {
asyncData({ $axios, store }) { //here we get Vuex and Axios
let id = store.state.id
return $axios.get(`url${id}`)
.then(...)
}
}https://stackoverflow.com/questions/57242101
复制相似问题