vue.js - 无法读取 store.js 中未定义的属性“帖子”
问题描述
我有一个store.js - vuex
被调用的动作signUserIn
。SignUserIn 方法只是一个http post
请求。我已导入Vue-Resource
in store
file: import VueResource from 'vue-resource'
,Vue.use(VueResource)
并且 in main.js
-store.js
已导入。在我的SignIn.vue
组件中,我调度SignIn
操作。我得到一个错误:Cannot read property 'post' of undefined
。我的导入或代码有什么问题?
行动:
signUserIn ({commit, getters}, payload) {
let data = {
_email: payload.email,
_password: payload.password
}
let that = this
that.$http.post(
'url',
data,
{ channel: 'default' },
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
).then(response => {
const newUser = {
email: payload.email,
login: payload.login,
cart: []
}
commit('setUser', newUser)
}, error => {
console.error(error);
});
}
突变:
setUser (state, payload) {
state.user = payload;
}
解决方案
$http
是 Vue 实例的属性,在 vuex 商店中你应该导入 Vue 并使用它Vue.http
来发出请求。
Vue.http.post(
'url',
data,
{ channel: 'default' },
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
)
...
推荐阅读
- fastify - fastify中如何分离控制器和服务?
- python - 打印功能不打印结果
- amazon-web-services - 使用 terraform 创建 AWS 账户
- azure-devops - 如何在自定义条件下同时在 Azure DevOps 发布管道中运行多个复制文件任务?
- c# - 使用 INotifyPropertyChanged 更改属性时如何执行代码
- vue.js - Vue 3 + ESLint 警告 [intlify] 检测到 HTML
- json - jsonformatter 显示无效的 json 格式
- oracle - 使用选择步骤和组时,OBIEE 计算数据透视表中的授予总计错误
- azure-devops - 跨项目克隆 Azure DevOps 工作项
- java - Domino 服务器:仅将 JVM 选项传递给 http 任务