programing

Vuex ReferenceError가 정의되지 않았습니다.값을 올바르게 설정하려면 어떻게 해야 합니까?

goodsources 2022. 8. 10. 22:24
반응형

Vuex ReferenceError가 정의되지 않았습니다.값을 올바르게 설정하려면 어떻게 해야 합니까?

Vuex에 signUp() 함수가 있는 작업이 있습니다. signupError 값을 설정하려면 어떻게 해야 합니까?

제가 시도한 것은 다음과 같습니다.commit(signupError, null)그리고.$state.commit(signupError, null) 그러나 "ReferenceError: signupError is not defined"라는 오류가 나타난다.

다음 예에서 signupError를 설정하려면 어떻게 해야 합니까?

store.displaces를 설정합니다.

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.discloss.main.discloss.

....

/* 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변환명과 payload를 전달합니다.

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;
    }
  }
}

또, 비동기 콜의 데이터에 대해서는 아무것도 하고 있지 않습니다.

언급URL : https://stackoverflow.com/questions/66027725/vuex-referenceerror-is-not-defined-how-do-i-set-a-value-correctly

반응형