L
L
low molecular macro2021-02-10 20:38:43
Vue.js
low molecular macro, 2021-02-10 20:38:43

Why doesn't rootState work?

There is a store with a built-in module

const counterModule = {
  namespaced: true,
  state() {
    return {
      counter: 0
    };
  },
  getters: {
    testAuth(rootState) {
      return rootState.isLoggedIn;
    }
  }
};

const store = createStore({
  modules: {
    counter: counterModule
  },
  state() {
    return {
      isLoggedIn: false
    };
  }
});


And component
<template>
  <p>{{ isTestAuth }}</p>
</template>

<script>
export default {
  computed: {
    isTestAuth() {
      return this.$store.getters['counter/testAuth'];
    }
  }
};
</script>

But for some reason the component does not display the content. If you don't use namespace in the plugin, then everything works correctly. What is the reason for the error?
PS most of the code that is not relevant to the topic of the question has been removed so as not to create confusion

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Yarkov, 2021-02-10
@yarkov Vue.js

And why did you decide that rootState is the first argument in the module getter? Because the argument was named rootState ?
Read until fully enlightened .
UPD: Well xs, everything works for me as usual

A
Anton Anton, 2021-02-10
@Fragster

The point is inattention or misunderstanding of what destructuring assignment is.

spoiler

Там на самом деле всего один параметр - объект, а все между скобок - его свойства.
4gtc4hpc8brc85oyyrb-paru0lw.png

F
fallus, 2021-02-11
@fallus

That's it - no need.
It should be like this, like this, like this:

getters: {
    testAuth(state, getters, rootState) {
      return rootState.isLoggedIn; // тут возможно нужно к isLoggedIn полный путь написать, например return rootState['myStoreModule/isLoggedIn']
    }
  }

*Don't hit me. No need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question