Error with Vue 2: getActivePinia was called with no active Pinia. Did you forget to install pinia? #833
Replies: 7 comments 3 replies
-
This error means you are trying to create a store (via useSomeStore()) before |
Beta Was this translation helpful? Give feedback.
-
Thanks for the info, looks like I got confused. Unfortunately I am not able to get this to work. I changed my implementation to: <template>
// Removed as not needed
</template>
<script lang="ts">
// ...Other imports
import { mapStores } from 'pinia'
import { useSettingsStore } from "@/store/settingsStore";
@Component({
components: {
Autocomplete,
Button
},
computed: {
...mapStores(useSettingsStore)
}
})
export default class SettingsView extends Vue {
// Not needed
get location(): string {
return this.settingsStore.location
}
// ...Other computed properties
}
</script>
but I get the following error:
I tried with this.settings as well but the same error is thrown. I followed the same process as described in this issue: vuejs/vue-class-component#109. I am afraid there is some point I am missing with the spread operator. Any hints or suggestions? |
Beta Was this translation helpful? Give feedback.
-
So I have done a bit more digging, and it looks like the state can be used without any major issues, as long as the keys are redefined: <template>
// Not needed
</template>
<script lang="ts">
// Other imports
import "@/types/api.types";
import { transportService } from '@/services/TransportService';
import { useSettings } from "@/store/settingsStore";
import { mapState } from "pinia";
@Component({
computed: {
...mapState(useSettings, {
location: 'location'
})
}
})
export default class Home extends Vue {
// Other fields
// For some reason, the field from the state has to be redefined here otherwise it throws errors
private location!: string;
async getStationBoard() {
const [error, stationBoardResponse] = await transportService.getStationBoard(this.location);
// Process the result
}
created() {
this.getStationBoard()
}
}
</script> Unfortunately I still cannot use the store directly, and I am afraid I am missing something very basic regarding the setup |
Beta Was this translation helpful? Give feedback.
-
Explicitly passing the pinia instance to useStore seems to work (?) import { createPinia, PiniaVuePlugin } from 'pinia'
Vue.use(PiniaVuePlugin);
const pinia = createPinia();
const useStore = defineStore('main', { ... })
const store = useStore(pinia) // Works, but not without passing pinia
new Vue({
pinia,
render: h => h(App)
}).$mount('#app') |
Beta Was this translation helpful? Give feedback.
-
Not sure if this helps, I had the same issue. Before my .use had vuetify and pinia in the same use statement. Once I broke it out like below it worked fine. createApp(App)
.use(createPinia())
.use(vuetify)
.component('font-awesome-icon', FontAwesomeIcon)
.mount('#app') |
Beta Was this translation helpful? Give feedback.
-
Other than the aforementioned solution, one could forget to initialized the store on the same project but with multiple pages(Multiple page Application) that was in my case |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. Can anybody help me please? |
Beta Was this translation helpful? Give feedback.
-
Reproduction
I have an app, written in vue 2 with TypeScript that previously used VueX for the state. I am trying to migrate to Pinia but I keep on getting the same error:
This is shown in the browser console. The project is built correctly, and the dev server starts with no problem, but when opening the page it is blank and the error is shown on the console.
I followed the example at https://github.com/piniajs/example-vue-2-vite, although I am not using Vite as of now (could that be an issue?). I installed both Pinia, and the recommended
@vue/composition-api
package using yarn. I also read about the usage without thesetup()
here: https://pinia.esm.dev/cookbook/options-api.htmlThis is my
main.ts
:My store is defined as follows:
And I am using it like so in my component (simplified):
Steps to reproduce the behavior
yarn serve
Additional information
package.json
Beta Was this translation helpful? Give feedback.
All reactions