Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pinia store does not initialise

I have a new Nuxt 3 project and wanted to add Pinia.
The store is created, but is not loaded. I do not receive an error message or warning.
The dev tools of both Nuxt and Vue show me that Pinia is installed. But I only see the Pinia (root) and no store.

package.json

{
  "name": "nuxt-app",
  ...
  "dependencies": {
    "@nuxt/types": "^2.17.3",
    "@nuxtjs/tailwindcss": "^6.12.0",
    "@pinia/nuxt": "^0.5.1",
    "@vueuse/core": "^10.10.0",
    "nuxt": "^3.11.2",
    "pinia": "^2.1.7",
    "primeicons": "^7.0.0",
    "primevue": "^3.52.0",
    "vue": "^3.4.27",
    "vue-router": "^4.3.2"
  },
  "devDependencies": {
    "@nuxt/eslint": "^0.3.13",
    "@vueuse/nuxt": "^10.10.0",
    "eslint": "^9.3.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.1.3",
    "eslint-plugin-vue": "^9.26.0",
    "nuxt-primevue": "^3.0.0",
    "prettier": "^3.2.5",
    "typescript": "^5.4.5",
    "vue-tsc": "1"
  }
}

nuxt.config.ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// https://nuxt.com/docs/api/configuration/nuxt-config
// eslint-disable-next-line no-undef
export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: [
    '@pinia/nuxt',
    '@nuxtjs/tailwindcss',
    'nuxt-primevue',
    '@nuxt/eslint',
    '@vueuse/nuxt'
  ],
  css: ['~/assets/css/base.css', '/node_modules/primeicons/primeicons.css'],
  primevue: {
    options: {
      unstyled: true
    },
    importPT: { as: 'Aura', from: '~/presets/aura' }     //import and apply preset
  },
  imports: {
    dirs: ['stores'],
    presets: [
      {
        from: 'pinia',
        imports: ['storeToRefs']
      }
    ]
  }
})

Folder structure

| stores
--| user.ts

user.ts

import { defineStore } from 'pinia'
import { useStorage } from '@vueuse/core'

export const useUserStore = defineStore('user', () => {
  const user = useStorage('user', { name: 'John Doe' })

  return {
    user
  }
})

>Solution :

OP thought that Pinia was not properly initialized, turns out you need to import a function in a component for it to appear in the devtools.

In their specific project, they did

<script setup>
import { useUserStore } from 'stores/user.js'

const userStore = useUserStore()
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading