简体   繁体   English

Vuex 操作无法提交突变

[英]Vuex action cannot commit mutation

I am working on an authentication function for a website, i am doing this with Firebase.我正在为网站开发身份验证功能,我正在使用 Firebase 执行此操作。 Whenever the user logs into firebase my action in my store gets triggered and commits my mutation so the state gets set with the userID.每当用户登录 firebase 时,我在我的商店中的操作都会被触发并提交我的更改,以便使用 userID 设置状态。 Somehow whenever i try to commit my mutation i keep getting the error: commit is not defined .不知何故,每当我尝试提交我的突变时,我都会收到错误消息: commit is not defined I can't seem to find a solution to this problem, all the forums i have been on havent helped so i really hope someone on here can help me with my problem, i would really appreciate it.我似乎无法找到这个问题的解决方案,我一直在的所有论坛都没有帮助,所以我真的希望这里有人可以帮助我解决我的问题,我真的很感激。

My action:我的行动:

  async login({ dispatch }, user) {
    const token = await auth.currentUser.getIdToken(true);
    const idTokenResult = await auth.currentUser.getIdTokenResult();

    let approved = false;
    const userData = await firestore.collection('users').doc(user.email).get();
    if (userData) {
      approved = userData.data().isApproved
    }

    const userInfo = {
      name: user.displayname || null,
      email: user.email || null,
      avatar: user.photoURL || null,
      uid: user.uid,
      approved
    };
    Cookies.set('access_token', token); // saving token in cookie for server rendering
    commit('saveUID', userInfo.uid);
  }
};

My mutation:我的突变:

saveUID (state, uid) {
    state.uid = uid;
}, 

The first parameter of the action is the context , which has functions like commit and dispatch .操作的第一个参数是context ,它具有commitdispatch等功能。 You extract ( destructuring assignment ) the dispatch by using { dispatch } as your parameter.您可以使用{ dispatch }作为参数提取( 解构赋值dispatch You can use { dispatch, commit } to fix this and actually assign commit to a local variable.您可以使用{ dispatch, commit }来解决此问题并将提交实际分配给局部变量。

destructuring assignment解构赋值

async login({ dispatch, commit }, user) {
  commit('your_mutation')
}

using context使用上下文

async login(context, user) {
  context.commit('your_mutation')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM