Как добавить модуль получения в composition api?

#vuejs2 #vue-composition-api

#vuejs2 #vue-composition-api

Вопрос:

Я использую vue 2, установленный composition api. Как я могу добавить геттеры? Обычно:

   computed: {
...mapGetters("Auth", ["isLogged"])}
 

………………………………………………………………..

 setup() {
const title_app = ref("Name App");
const logout = () => {
  store
    .dispatch("Auth/logout")
    .then(() => {
      router.push({ name: "About" });
    })
    .catch((err) => {
      console.log(err);
    });
};

return {
  title_app,
  logout,
};
 

},

Ответ №1:

Вы можете сделать что-то вроде этого:

 import { computed } from 'vue'

export default {
  setup (props, { root }) {
    const isLogged = computed(() => root.$store.Auth.getters.isLogged)

    return {
      isLogged
    }
  }
}
 

Комментарии:

1. «Ошибка типа: root.$store. Auth не определен »

2. попробуйте отладить его, какая его часть не определена.

3. root.$store.getters['Auth/isLogged'] будет работать