Vuex ReferenceError 未定义。如何正确设置值?
Vuex ReferenceError is not defined. How do I set a value correctly?
我在 Vuex 中有一个函数 signUp() 如何设置 signupError 的值?
这是我尝试过的方法:commit(signupError, null)
和 $state.commit(signupError, null)
但是我收到错误消息“ReferenceError: signupError is not defined”
如何在以下示例中设置 signupError?
store.js
import Vuex from "vuex";
import Vue from "vue";
import { Auth } from "aws-amplify";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: null,
signupError: null
},
actions: {
async signUp({ commit }, { username, password, firstName, lastName }) {
commit(signupError, null)
try {
const data = await Auth.signUp({
username,
password,
attributes: {
email: username
}
});
} catch (error) {
state.signupError = err.message || err
console.log('error signing up:', error);
return error;
}
main.js
....
/* eslint-disable no-new */
new Vue({
el: '#app',
store: store, // Vuex mechanism to "inject" the store into all child components from the root component.
render: h => h(App),
router
})
reg.vue
....
await this.$store.dispatch('signUp', {....
您缺少一个变更,但调用 commit
需要提交一个变更。
mutations: {
SET_ERROR(state, value) {
state.signupError = value;
}
}
当您调用 commit
时,将变异名称和有效载荷传递给它:
commit('SET_ERROR', null);
您还需要在 catch
块中使用它,因为您无法通过操作设置 state
:
commit('SET_ERROR', error.message || error);
您同时使用了err
和error
,请确保保持一致
整个动作:
actions: {
async signUp({ commit }, { username, password, firstName, lastName }) {
commit('SET_ERROR', null);
try {
const data = await Auth.signUp({
username,
password,
attributes: {
email: username
}
});
} catch (error) {
commit('SET_ERROR', error.message || error);
console.log('error signing up:', error);
return error;
}
}
}
此外,您没有对来自异步调用的数据执行任何操作。
我在 Vuex 中有一个函数 signUp() 如何设置 signupError 的值?
这是我尝试过的方法:commit(signupError, null)
和 $state.commit(signupError, null)
但是我收到错误消息“ReferenceError: signupError is not defined”
如何在以下示例中设置 signupError?
store.js
import Vuex from "vuex";
import Vue from "vue";
import { Auth } from "aws-amplify";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: null,
signupError: null
},
actions: {
async signUp({ commit }, { username, password, firstName, lastName }) {
commit(signupError, null)
try {
const data = await Auth.signUp({
username,
password,
attributes: {
email: username
}
});
} catch (error) {
state.signupError = err.message || err
console.log('error signing up:', error);
return error;
}
main.js
....
/* eslint-disable no-new */
new Vue({
el: '#app',
store: store, // Vuex mechanism to "inject" the store into all child components from the root component.
render: h => h(App),
router
})
reg.vue
....
await this.$store.dispatch('signUp', {....
您缺少一个变更,但调用 commit
需要提交一个变更。
mutations: {
SET_ERROR(state, value) {
state.signupError = value;
}
}
当您调用 commit
时,将变异名称和有效载荷传递给它:
commit('SET_ERROR', null);
您还需要在 catch
块中使用它,因为您无法通过操作设置 state
:
commit('SET_ERROR', error.message || error);
您同时使用了err
和error
,请确保保持一致
整个动作:
actions: {
async signUp({ commit }, { username, password, firstName, lastName }) {
commit('SET_ERROR', null);
try {
const data = await Auth.signUp({
username,
password,
attributes: {
email: username
}
});
} catch (error) {
commit('SET_ERROR', error.message || error);
console.log('error signing up:', error);
return error;
}
}
}
此外,您没有对来自异步调用的数据执行任何操作。