I’m building a Vue app using the OptionsAPI along with a Pinia Store but I frequently run into an issue stating that I’m trying to access the store before createPinia()
is called.
I’ve been following the documentation to use the Pinia store outside components as well, but maybe I’m not doing something the proper way.
Situation is as follows:
I have a login screen (/login
) where I have a Cognito session manager, I click a link, go through Cognito’s signup process, and then get redirected to a home route (/
), in this route I also have a subroute that shows a Dashboard
component where I make an API call.
On the Home
component I call the store using useMainStore()
and then update the state with information that came on the URL once I got redirected from Cognito, and then I want to use some of the state information in the API calls inside Dashboard
.
This is my Home
component, which works fine by itself, due to having const store = useMainStore();
inside the mounted()
hook which I imagine is always called after the Pinia instance is created.
<template>
<div class="home">
<router-view></router-view>
</div>
</template>
<script>
import {useMainStore} from '../store/index'
export default {
name: 'Home',
components: {
},
mounted() {
const store = useMainStore();
const paramValues = {}
const payload = {
// I construct an object with the properties I need from paramValues
}
store.updateTokens(payload); // I save the values in the store
},
}
</script>
Now this is my Dashboard
component:
<script>
import axios from 'axios'
import {useMainStore} from '../store/index'
const store = useMainStore();
export default {
name: "Dashboard",
data() {
return {
user_data: null,
}
},
mounted() {
axios({
url: 'myAPIUrl',
headers: { 'Authorization': `${store.token_type} ${store.access_token}`}
}).then(response => {
this.user_data = response.data;
}).catch(error => {
console.log(error);
})
},
}
</script>
The above component will fail, and throw an error stating that I’m trying to access the store before the instance is created, I can solve this just by moving the store declaration inside the mounted()
hook as before, but what if I want to use the store in other ways inside the component and not just in the mounted
hook? And also, why is this failing? By this point, since the Home
component already had access to the store, shouldn’t the Dashboard
component, which is inside a child route inside Home
have the store instance already created?