Pinia throws a useStore is not callable error

I have been trying to resolve the issue with

(0 , pinia__WEBPACK_IMPORTED_MODULE_1__.useStore) is not a function

but unfortunately, I haven't been able to find a solution. Can anyone point out what mistake I am making?

Here is my store.js code:

import { defineStore } from "pinia";

export const myStore = defineStore("myStore", {
  state: () => ({
    count: 0,
    message: "Hello, Pinia!",
  }),
  actions: {
    increment() {
      this.count++;
    },
    updateMessage(newMessage) {
      this.message = newMessage;
    },
  },
});

And here is my App.vue code:

<template>
  <div>
    <p>Count: {{ store.count }}</p>
    <button @click="store.increment">Increment</button>
  </div>
</template>

<script>
import { defineComponent } from "vue";
import { useStore } from "pinia";

export default defineComponent({
  setup() {
    const store = useStore("myStore");

    return {
      store,
    };
  },
});
</script>

Answer №1

useStore is not a function provided by Pinia. It should be eliminated:

import { useStore } from "pinia";

When referring to creating a useStore function in the documentation, it means whatever you export from your store.js file. In this case, it's myStore, but conventionally it should be named useMyStore.

export const useMyStore = defineStore("myStore", { ... })

After defining, import where necessary:

import { useMyStore } from 'store.js'

const store = useMyStore()

Answer №2

Implement pinia middleware before implementing router middleware.

This order is essential for proper functionality.

app.use(router);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Increment and decrement the like count on fa-heart multiple times

Is there a way to increment the count of a fa-heart value on click and decrement it on the second click? The issue I'm facing is that I have multiple fa-heart elements on the same page, making it challenging to increment or decrement the clicked fa-h ...

The absence of a semi-colon in JSLint

I encountered an error message indicating a semicolon is missing, however I am unsure of where to place it. Here is the snippet of code: $('.animation1').delay(350).queue(function(){ $(this).addClass("animate-from-top") }); ...

How to attach an event listener to an input element using Angular

I am looking to add a listener to an input element that will be triggered every time the user changes the input values. The goal is to display the current values chosen by the user. Example HTML template: <div id="idDoseLabel1" class="da ...

"Alert: Jest has detected that an update to MyComponent in a test was not properly enclosed in an act() function, even though there was no actual update being made. Please

Here's a super straightforward test I've put together: it('renders', () => { const { toJSON } = render( <MockedProvider> <MyComponent /> </MockedProvider> ) expect(toJSON()).toMatc ...

Vue.js event handlers

I am in the process of creating a basic app that will include a few listeners. However, I am struggling to determine the best approach for organizing the logic behind it. Here is an example of the HTML: <div id="playerProgressBar"> <div id=" ...

Working on executing various methods with a JavaScript client on a Flask RESTful API that requires authentication

I'm currently developing a JavaScript client-side app to interact with a Flask RESTful API. I've implemented some methods that require user authentication, but for some reason, even after logging into the server, I receive a 401 error (Unauthoriz ...

Transform an HTML table string into JSON format

I discovered a useful library called table to JSON that allows me to convert an HTML Table to JSON using its ID (#testtable in the example below): var table = $('#testtable').tableToJSON(); alert(JSON.stringify(table)); However, I am interested ...

DataGrid React MUI: Aligning Column Data and Header for "Number" Type

In my React project, I am using MUI. I noticed that when I specify the type of a column as type: "number", both the column header and data align to the right. This issue can be replicated in a simple example taken from the MUI documentation: code ...

Send the output of an asynchronous ajax request to the designated function within the jQuery Validation plugin

Utilizing the jQuery Validation plugin (JVP) for server-side validations via ajax has been seamless, except for one hitch: JVP struggles with performing remote validation on blank fields. While it's easy to use its default required: true rule for sim ...

What is the recommended data type for the component prop of a Vuelidate field?

I'm currently working on a view that requires validation for certain fields. My main challenge is figuring out how to pass a prop to an InputValidationWrapper Component using something like v$.[keyField], but I'm unsure about the type to set for ...

Is it possible for Angular.js timer finish event not to trigger data binding correctly?

I've been working on an AngularJS application that functions as a quiz by displaying pictures and prompting users to select the correct answer by clicking a button. The app is designed to store the user's answers in an object. Everything seems t ...

I am experiencing an issue where my JSON array is only returning the last element. Any suggestions on how to

I am facing an issue with my JSON array and Ajax code. Here is the snippet of my code where I upload an Excel file, convert it to JSON, then save it as a string in my database: function exportExcelToTable() { $('#upload-excel-convert').chang ...

Making JSON function in Internet Explorer

I'm encountering an issue retrieving data from a JSON feed specifically in Internet Explorer. Here's the problem. It functions correctly in Firefox, Chrome, and Safari, but fails to alert in IE: function perform_action(data){ alert(data); } ...

Tips for transforming a string into an object using AngularJS

Here is a string I'm working with: $scope.text = '"{\"firstName\":\"John\",\"age\":454 }"'; I am trying to convert it into a JavaScript object: $scope.tmp = {"firstName":"John","age":454 }; Please note: J ...

Ways to prevent the Vue component from triggering the mounted hook

Here is a snippet of code that showcases how I am using mixins and components in my project: export default { created () { if (!this.$store.getters.isAuthenticated) { this.$router.replace('/start') } } } And here is how I am im ...

Having trouble with your custom AngularJS directive not functioning correctly?

I am facing an issue with my custom directive implementation. I have a custom directive that contains a table which references a controller. The ProjectController part works fine when it is not included in the code, but as soon as I put everything into the ...

When the page is resized for mobile, the Bootstrap modal shifts downwards

I am encountering an issue with a modal that pops up on my webpage when clicked. It is perfectly centered in a browser view and smaller views. However, when I resize the page for phones, the modal shifts down the page and requires scrolling to see it. How ...

The responsiveness of Vuetify's v-data-table is compromised when utilizing the v-slot:body feature

Issue with Vuetify v-data-table not stacking on mobile devices when using the body v-slot Is there a way to make the data table stack properly even when it has the body v-slot implemented? The normal behavior of a v-data-table can be seen in the Vuetify d ...

I have noticed that the Javascript code is being loaded prior to the completion of my HTML/CSS page for some unknown

I am completely new to the world of web development. I'm encountering an issue where my JavaScript code (specifically alerts) is loading before my HTML/CSS page has fully loaded. I've been using sample alerts to test this out, and ideally, they s ...

Tips for preserving an HTML dynamic table in a database

I have a challenge where I am generating a dynamic HTML table using javascript. What is the best way to store this dynamic HTML table in a database using CodeIgniter? This is the code snippet for my input form: <table id="tb3" name="tb3"> & ...