首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asyncData中的Vuex和Axios

asyncData中的Vuex和Axios
EN

Stack Overflow用户
提问于 2019-07-28 15:02:33
回答 1查看 297关注 0票数 2

我想在asyncData中访问我的Vuex数据,但是我不能访问,而且我似乎不能在asyncData中使用axios模块。

我试了很多次。

pages/index.vue

代码语言:javascript
复制
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

代码语言:javascript
复制
export const state = () => ({
  id: "#5nkI12_fSDAol/_Qa?f"
})

我希望它从Vuex获得ID,然后执行Axios req。整个应用程序都不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-28 15:11:47

您不能在this中使用asyncData。函数asyncData作为param传递一个上下文对象,它基本上表示this关键字。

首先,您需要指定要从context对象检索什么,然后可以使用它。

在你的例子中,做这样的事情:

代码语言:javascript
复制
export default {
  asyncData({ $axios, store }) { //here we get Vuex and Axios
    let id = store.state.id
    return $axios.get(`url${id}`)
      .then(...)
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57242101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档