Vue,错误[vuex]不会在变异处理程序之外变异vuex存储状态

时间:2020-07-11 03:18:53

标签: vue.js vuex nuxt.js

这是原始代码。非常简单,请登录表单。我们有电子邮件字段,密码。它使用此参数,然后单击“提交”按钮,它将检查用户并将其user.uid写入Vuex。但是我收到了上面标题中列出的错误。我做了研究,这似乎是Vuex中的一个常见问题,由于这些字段有时会“即时”更新Vuex存储,这在我的情况下是错误的,因为它仅在您按下提交按钮时才更新Vuex存储。无论如何,决定改正为this,但仍然没有运气

原始代码

<template>
      <div class="form__inner">
          <div class="overlay" @click="$store.commit('logReg/logIn')"></div>
          <v-container
            fill-height
            fluid>
            <v-row
              align="center"
              justify="center">
              <v-col cols="2">
                <v-card>
                  <v-card-title>
                    Log in
                  </v-card-title>
                  <v-card-text>
                    <v-text-field placeholder="Email"
                                  v-model="logIn"/>
                    <v-text-field placeholder="Password"
                                  v-model="password"/>
                  </v-card-text>
                  <v-card-actions>
                    <v-btn color="success"
                           class="mx-auto"
                           @click="signIn">Log me in</v-btn>
                  </v-card-actions>
                </v-card>
              </v-col>
            </v-row>
          </v-container>
      </div>
    
    </template>
    
    <script>
      export default {
        data(){
          return {
            logIn: '',
            password: ''
          }
        },
        methods:  {
          async signIn(){
            let res = await this.$fireAuth.signInWithEmailAndPassword(this.logIn, this.password);
            this.$store.commit('userState', res);
          }
        }
      }
    </script>

我的vuex

export const state = () => ({
  user: null
})

export const mutations = {
  userState(state, authUser){
    state.user = authUser;
  }
}

我尝试修复仍然没有运气的问题,并给出相同的错误

<template>
  <div class="form__inner">
      <div class="overlay" @click="$store.commit('logReg/logIn')"></div>
      <v-container
        fill-height
        fluid>
        <v-row
          align="center"
          justify="center">
          <v-col cols="2">
            <v-card>
              <v-card-title>
                Log in
              </v-card-title>
              <v-card-text>
                <v-text-field placeholder="Email"
                              v-model="logIn"/>
                <v-text-field placeholder="Password"
                              v-model="password"/>
              </v-card-text>
              <v-card-actions>
                <v-btn color="success"
                       class="mx-auto"
                       @click="signIn">Log me in</v-btn>
              </v-card-actions>
            </v-card>
          </v-col>
        </v-row>
      </v-container>
  </div>

</template>

<script>
  export default {
    data(){
      return {
        logIn: '',
        password: ''
      }
    },
    computed: {
      logg: {
        get(){
          return this.logIn;
        },
        set(val){
          this.logIn = val;
        }
      },
      pass: {
        get(){
          return this.password;
        },
        set(val){
          this.password = val;
        }
      }
    },
    methods:  {
      async signIn(){
        let res = await this.$fireAuth.signInWithEmailAndPassword(this.logg, this.pass);
        this.$store.commit('userState', res);
      }
    }
  }
</script>

1 个答案:

答案 0 :(得分:0)

您必须使用这样的突变:

<script>
  import {mapMutations} from 'vuex';
  export default {
    data(){
      return {
        logIn: '',
        password: ''
      }
    },
    methods:  {
      ...mapMutations({
           userState: 'userState'
      }),
      async signIn(){
        let res = await this.$fireAuth.signInWithEmailAndPassword(this.logIn,this.password);
        this.userState(res);
      }
    }
  }
</script>