我使用vue-cookie和vue-resource创建项目,尝试使用this.$cookies.get('my-cookie')
访问cookies,并使用this.$http.post('URL')
请求API。商店无法访问this
。
我在store.js中的行为:
checkCookies: () => {
if (this.$cookies.get('test') === null) {
console.log('vide')
//envoyer sur la page de connexion
} else {
console.log('pas vide')
this.$http.post('URL', {
})
}
}
如何调用我的操作(这是方法调用单击):
test() {
return this.$store.dispatch('checkCookies')
}
如何调用我的测试函数:
<v-btn @click="test">cookies!</v-btn>
运行checkCookies
时出现了以下错误:
v-on处理程序中的
错误:"TypeError:_this未定义“
我通常会在我的商店里使用this
,我知道我可以一个一个地导入每个包来使用它们,但是,我想知道是否有在vuex中使用this
的方法?
发布于 2020-06-25 11:29:41
this.$cookies
和this.$http
在组件方法中工作,因为这些函数是通过各自的插件添加到Vue.prototype
中的。
无论如何,这些只是方便的方法;看起来这些插件直接在Vue
上安装等效的方法:
this.$http -> Vue.http
this.$cookies -> Vue.$cookies
如果您真的希望能够在Vuex操作中执行this.$http
,则需要将这些方法分配给存储区:
const store = new Vuex.Store({ ... })
// Make sure you have registered the Vue plugins by this point
store.$http = Vue.http
store.$cookies = Vue.$cookies
https://stackoverflow.com/questions/62532364
复制相似问题