2
votes

I have an Vue App. I use vuex. I created my app like this:

import { createApp } from "vue";
import axios from "axios";
import App from "./App.vue";
import router from "./router";
import store from "./store/index";

axios.defaults.baseURL = "https://localhost:44349";

const app = createApp(App)
  .use(router)
  .use(store)
  .mount("#app");

Than i one of my component i am trying to access context.root.$store in the setup() method , but context.root is undefined.

<script>
import {ref, computed } from "vue";

export default {
  name: "ClientList",

  setup(props, context) {
    
    const clients = ref([]);

    console.log(context);

    const clientOrdersLenght = computed(() => {
      return clients.value.length;
    });


    return { clients, clientOrdersLenght }
  },
  
};
</script>

My idea is get acces to my store via context.root. I watched videos and examples with this. but they refer to Vue 2 using 'vue/composition-api' as import.

What i am missing?

2

2 Answers

0
votes

You might be able to include and access the store directly

store.dispatch('myModule/myModuleAction') store.myModule.state.blabla

import {ref, computed } from "vue";
import store from './store/index'; // include store

export default {
  name: "ClientList",

  setup(props) {
    
    const clients = ref([]);

    const clientOrdersLength = computed(() => {
      store.dispatch('myAction'); // call dispatch
      store.state.myItem // or access store's state
      return clients.value.length;
    });

    return { clients, clientOrdersLength }
  },
  
};
-1
votes

I found an easy way to access my store with getStore() from vuex. That's cool. But for other reason in the feature, me or somebody else will need to access the $root (vue instance) of the app. So may be now the correct question is how to get the $root (vue instance) of the app in a child component?