Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly initialize Vuex store on app startup using rest API?

I want to initialize the store with some data that is fetched from API (I'm using axios) How can I do this on app startup, only once?

I have a store.js file that exports my store and a main.js file that does this:

import Vue from "nativescript-vue";
import store from "./store";

new Vue({
  store,
  render: (h) => h("frame", [h(App)]),
}).$start();
like image 715
yukashima huksay Avatar asked Oct 23 '25 17:10

yukashima huksay


1 Answers

Try using .dispatch() with your Axios REST API grabbing function in the created() hook in the main Vue component (the one you included store in):

new Vue({
  store,
  render: (h) => h("frame", [h(App)]),
  created() {
    this.$store.dispatch('myAxiosFetchFunction');
  },
}).$start();
like image 52
Zaroth Avatar answered Oct 25 '25 11:10

Zaroth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!