NuxtN
Nuxt2y ago
whatstaz

Where to fetch initial user data

What is the best practice to load initial user data? Currently I do it in the default.vue layout in an onMounted hook, but that doesn't seem like the right way as sometimes it doesn't work.

Here is the code:
const userStore = useUserStore()

const { user } = storeToRefs(userStore)

const accountStore = useAccountsStore()
const categoryStore = useCategoryStore()
const payeeStore = usePayeeStore()
const transactionStore = useTransactionStore()

onMounted(() => {
  if (!accountStore.isInitialized) {
    accountStore.getAccounts()
    accountStore.initialized()
  }
  if (!categoryStore.isInitialized) {
    categoryStore.getCategories()
    categoryStore.initialized()
  }
  if (!transactionStore.isInitialized) {
    transactionStore.getTransactions()
    transactionStore.initialized()
  }
  if (!payeeStore.isInitialized) {
    payeeStore.getPayees()
    payeeStore.initialized()
  }
})


Any help would be greatly appreciated!
Was this page helpful?