Vue.js Vuex操作中的Vue资源-“;这是空的;错误
我正在尝试从一个商店内部打一个GET电话。但是,我的Vue.js Vuex操作中的Vue资源-“;这是空的;错误,vue.js,vuejs2,vuex,vue-resource,Vue.js,Vuejs2,Vuex,Vue Resource,我正在尝试从一个商店内部打一个GET电话。但是,我的Vue.http.get()调用抛出一个TypeError“this is null” 我完全不知所措,在其他地方我还没有找到其他人有这个问题。任何帮助都将不胜感激。谢谢 演练 用户导航至/登录/ 提交登录名 已验证并收到令牌 分派操作以保存令牌并检索配置文件 尝试使用Vue.http内部操作时出错 注意,令牌正确地存储在state和localStorage中 Login.vue 商场 auth.js-存储模块 堆栈跟踪 TypeError:这
Vue.http.get()
调用抛出一个TypeError
“this is null”
我完全不知所措,在其他地方我还没有找到其他人有这个问题。任何帮助都将不胜感激。谢谢
演练
Vue.http
内部操作时出错TypeError:这是空的
堆栈跟踪:
@网页包-internal:///10:42:9
exec@webpack-internal:///6:1150:21
Client/结果是我的Vue实例中的代码导致了问题。在那里,我将csrf值推送到X-CSRFTOKEN
头
我正在使用Django和DRF。我刚刚从SessionAuthentication
切换到使用TokenAuthentication
,现在我了解到不需要传递csrf令牌
下面的代码来自mymain.js
,删除它后问题就解决了
违规代码
确切地说,这导致抛出TypeError
。你的setProfile突变是什么样子的?我已经更新了这个问题,并在auth.js代码中添加了更多细节。感谢错误只显示“这是空的”@andresgottlieb关于调度“auth/save”。据我所知,这是如何分派到命名空间存储模块的操作。我能找到的最接近的文档是“带名称空间的绑定帮助程序”,我投票将这个问题作为离题题来结束,因为这个问题是特定于我自己的代码的,并且通常不会有影响。因为这是一些特定于代码的问题,如果你关闭或删除你的问题,这将是最好的-它不会给任何人带来任何帮助,因为问题是完全不同的。这只会引起混乱,你说得很对。感谢大家的时间和关注。祝Vue好运!:)
methods: {
submit() {
this.$http.post(
this.loginEndpoint,
this.object
).then(response => {
this.$store.dispatch({
"type": "auth/save",
"token": response.body.key,
}).then(() => {
this.$router.push({name: "home"})
}).catch(error => {
console.log(error)
})
}).catch(error => {
console.log(error)
})
}
}
import Vue from 'vue'
import Vuex from 'vuex'
import Auth from '../stores/auth.js'
// plugins
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
auth: Auth,
},
})
import Vue from 'vue'
export default {
namespaced: true,
state: {
token: null,
profile: {},
},
mutations: {
setProfile(state, payload) {
state.profile = payload.profile
},
setToken(state, payload) {
state.token = payload.token
},
},
actions: {
save: (context, payload) => {
return new Promise((resolve, reject) => {
const url = "/rest-auth/user/"
context.commit('setToken', {
token: payload.token,
})
localStorage.setItem("token", payload.token)
console.log("get user profile")
// *** Error below calling Vue.http.get ***
// TypeError: this is null
Vue.http.get(url).then(response => {
console.log("Profile received")
context.commit('setProfile', {
profile: response.body,
})
localStorage.setItem("profile", response.body)
resolve()
}).catch(error => {
reject(error)
})
})
},
},
}
TypeError: this is null
Stack trace:
@webpack-internal:///10:42:9
exec@webpack-internal:///6:1150:21
Client/<@webpack-internal:///6:1179:13
PromiseObj@webpack-internal:///6:200:24
Client@webpack-internal:///6:1143:16
Http@webpack-internal:///6:1400:12
Http[method$$1]@webpack-internal:///6:1431:16
save/<@webpack-internal:///14:112:17
save@webpack-internal:///14:90:20
wrappedActionHandler@webpack-internal:///7:604:15
dispatch@webpack-internal:///7:347:7
boundDispatch@webpack-internal:///7:272:12
submit/<@webpack-internal:///17:100:17
/* Add the csrf token to the request header */
Vue.http.interceptors.push(function(request, next) {
let token = this.$cookie.get("csrftoken")
request.headers.set('X-CSRFTOKEN', token)
next()
});