Pinia store seems to be loading out of order #1785
-
My SPA application is using a Pinia store to keep some API results. Here are some snippets and directory structure to illustrate my problem: In main.ts I have: import { createApp } from 'vue'
import { createPinia } from 'pinia'
import OktaVue from '@okta/okta-vue'
import { OktaAuth } from '@okta/okta-auth-js'
import App from './App.vue'
import router from './router'
import oktaConfig from './okta.config'
const oktaAuth = new OktaAuth(oktaConfig.oidc)
const app = createApp(App)
app.use(OktaVue, { oktaAuth })
app.use(router)
app.use(createPinia())
app.mount('#app') Here, In stores/group.ts I have: import { defineStore } from 'pinia'
import { getGroups } from '../api/api'
export const useGroupStore = defineStore('group', () => {
....
const groups = ref({})
async function fetchGroups(force: boolean = false) {
....
const response = await getGroups()
groups.value = response
.....
} In api/api.ts I have: import { mande, defaults } from 'mande'
import { useAuth } from '@okta/okta-vue'
....
const groups = mande(`${api_base}/groups`)
const auth = useAuth()
async function getGroups(groupID?: string) {
return await groups.get(groupID)
}
.... In components/MyView.vue: ....
<script setup lang="ts">
import { useGroupStore } from '../stores/group'
import { onMounted } from 'vue'
const store = useGroupStore()
onMounted(() => {
store.fetchGroups()
})
</script> This component is bound the /groups route. At this point I get an exception thrown from the line I would expect that none of the stores code would be run until main.ts completes and mounts the vue app. Not sure where to go from here, any advice is appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The error comes from https://github.com/okta/okta-vue#readme: you seem to be using the composable |
Beta Was this translation helpful? Give feedback.
The error comes from https://github.com/okta/okta-vue#readme: you seem to be using the composable
useAuth()
outside of a setup which isn't supported