Home > Back-end >  Pinia getter does not update
Pinia getter does not update

Time:03-03

I have following code

<script lang="ts">
import { RouterView } from "vue-router";
import defaultLayout from "@/layouts/default.vue";
import dashboardLayout from "@/layouts/dashboard.vue";
import { useDefaultStore } from "@/stores/default";
import "./index.css";
import { defineComponent } from "vue";
export default defineComponent({
  setup() {
    let { getLayout } = useDefaultStore();

    return { getLayout };
  },
  components: { defaultLayout, dashboardLayout },
});
</script>

<template>
  {{ getLayout }}
  <component :is="getLayout">
    <RouterView />
  </component>
</template>

When i got to /dashboard my state gets updated but the getter does not for some reason, why is that?

<script setup lang="ts">
import { useDefaultStore } from "@/stores/default";
let { getUserData, SET_LAYOUT, getLayout } = useDefaultStore();
SET_LAYOUT("dashboardLayout");
</script>

Here my store:

actions: {
    SET_LAYOUT(layout: any) {
      console.log("setting layout");
      console.log(layout);
      this.layout = layout;
    },
}

I literally can see the changes inside the console but they does not get applied on the UI

CodePudding user response:

I have found it out.

You cannot destructure your store, it loses its reactivity

let { getLayout } = useDefaultStore();

So i changed it to

let store = useDefaultStore();

and used store.getLayout and it works!

CodePudding user response:

This is covered by the documentation, this is the case for storeToRefs helper:

const { getLayout } = storeToRefs(useDefaultStore());
  • Related