3

I have a vue 3 (Javascript) project. Trying to split my vuex store to modules but I keep getting the error [vuex] unknown getter: loginToken. As far as I understand the only [major] change with vuex 4.0.0-beta is the import statement. How one uses modules should still be the same?

store/index.js

import { createStore } from 'vuex';
import * as auth from "./modules/auth";

const store = createStore({
  modules: {
    auth,
  },
  state: {
    ....
  },
  getters: {
    ...
  },
  mutations: {
    ...
  },
  actions: {
    ...
  }
})

store/modules/auth.js

import firebase from "./../../firebase";

const state = {
  loginToken: null,
}

const getters = {
  loginToken({ loginToken }) {
    return loginToken;
  },
}

const mutations = {
  loginToken(state, token) {
    if(token) {
      state.loginToken = token;
    } else {
      state.loginToken = null;
    }
  }
}

const actions = {
  login(context, request) {
    return new Promise((resolve, reject) => {
      firebase.auth().signInWithEmailAndPassword(request.email, request.password)
        .then(({ user }) => {
          context.dispatch("getUserProfile", user.uid).then(() => {
            context.commit("loginToken", user.uid);
            resolve(user.uid);
          });
        }).catch(function(error) {
        reject(error)
      });
    })
  }
}

export default { state, getters, mutations, actions }

Page.vue

<template>
  <ion-header>
    <ion-toolbar>
      <ion-buttons slot="secondary">
        <ion-button @click="gotoProfile" v-if="loginToken">Profile</ion-button>
        <ion-button @click="logout" v-if="loginToken">Logout</ion-button>
      </ion-buttons>
      <ion-buttons>
      </ion-buttons>
      <ion-title class="logo" @click="gotoHome">{{ name }} </ion-title>
    </ion-toolbar>
  </ion-header>
</template>
<script>
import { mapGetters } from 'vuex';
  ....
  computed: {
    ...mapGetters(['loginToken']),
  },
  ...
</script>
4
  • Can you add a code example of how you are using the getters? Commented Oct 15, 2020 at 7:22
  • 1
    did you try out ...mapGetters(['auth/loginToken']),? Commented Oct 15, 2020 at 8:57
  • @BoussadjraBrahim when I use auth/loginToken I get a warning now. Property "loginToken" was accessed during render but is not defined on instance. how do I use loginToken? Commented Oct 15, 2020 at 9:30
  • For me this solution doesn't work. stackoverflow.com/questions/66516327/… Commented Mar 9, 2021 at 18:53

1 Answer 1

4

imports in Vue.js 3 need to be like this:

import auth from "./modules/auth";

but you have it as:

import * as auth from "./modules/auth";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.