VUE3 中使用 VUEX,报错【[Vue warn]: inject() can only be used inside setup() or functional components.】
参考链接1:参考链接 vuex 报错 injuce()
参考链接2:vuex 报错 无法在methods中使用store的引入
-
因为在setup中,已经对store进行了定义,所以不能再在外边的methods中进行访问
-
javascript fail
···
import { useStore } from 'vuex';
···
setup () {
···
const store = useStore();
store.dispatch(`user/${LOGIN}`, { ...values, type: state.customActiveKey === 'tab1' }).then(res => {
console.log('========校验通过', res);
})
···
},
methods: {
handleTestStore () {
const store = useStore();
console.log(store, '=======', useStore);
const p = { info: 'test' };
useStore.dispatch(`user/${LOGIN}`, p).then(r => {
console.log('========校验通过', r);
});
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210402102317600.png#pic_center)
-
如何正确访问store,仅需要在methods中使用 this.$store,即可替代原先methods中的userStore,正确拿到store
-
···
methods: {
handleTestStore () {
console.log(this.$store)
const p = { info: 'test' };
this.$store.dispatch(`user/${LOGIN}`, p).then(r => {
console.log('========校验通过', r);
});
}
}
···
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210402102332897.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1dhbmdZQ18=,size_16,color_FFFFFF,t_70#pic_center)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)