【发布时间】:2020-10-09 03:15:35
【问题描述】:
我正在使用 VueJS 和 Vuex。我通过这种方式将用户 ID 放入商店: vuex screenshot
我尝试将用户 ID 传递给 fetch,但 vuejs 返回错误
([Vue warn]: Error in created hook: "TypeError: this.$store is 未定义")
import { LOAD_APPOINTMENTS } from './types'
export default {
loadProducts ({ commit }) {
var user = this.$store.state.user.userid
fetch('api/appointments/' + user)
.then((result) => {
return result.json()
})
.then((appointments) => {
commit(LOAD_APPOINTMENTS, appointments)
})
.catch(er => {
console.log(er)
})
}
}
【问题讨论】:
-
您如何/在哪里调用
loadProducts以及您提供的脚本的文件位置是什么?是某种.js文件,它只是导出该函数吗? -
我提供的脚本是 action.js,我在 App.Vue 中调用 loadProducts。这样:
-
此操作是
user模块还是存储根目录的一部分? -
@Phil 不,我有两个商店模块(用户和产品),模块用户将用户 ID 和用户名加载到 vuex 商店,产品从 api 获取并在 vuex 商店中为该用户加载产品跨度>
-
那么这个动作在哪个模块中?