Pinia: Difference between revisions

From AWVVO
Jump to navigationJump to search
Created page with "= Add Pinia to Vue = == Installation == <syntaxhighlight lang="php" copy> npm install pinia </syntaxhighlight> == Initialize == <syntaxhighlight lang="php" copy> import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' import vuetify from './plugins/vuetify' // Adjust if necessary const app = createApp(App) app.use(createPinia()) app.use(vuetify) app.mount('#app') </syntaxhighlight> == Create store src/stores/counter.js == <s..."
 
 
Line 42: Line 42:
   }
   }
})
})
</syntaxhighlight>
== Usage ==
<syntaxhighlight lang="php" copy>
<script setup>
import { useCounterStore } from '@/stores/counter'
const counterStore = useCounterStore()
</script>
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 05:22, 29 January 2025

Add Pinia to Vue

Installation

npm install pinia

Initialize

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import vuetify from './plugins/vuetify' // Adjust if necessary

const app = createApp(App)

app.use(createPinia())
app.use(vuetify)

app.mount('#app')

Create store src/stores/counter.js

import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    doubleCount: (state) => state.count * 2
  },
  actions: {
    increment() {
      this.count++
    },
    decrement() {
      this.count--
    }
  }
})

Usage

<script setup>
import { useCounterStore } from '@/stores/counter'

const counterStore = useCounterStore()

</script>