English
Mini App has Pinia built in.
Pinia (pronounced /piːnjʌ/
, like peenya
in English) is a repository for Vue that allows you to share state across components, pages. On the server side as well as in small single page applications, you can also get a lot of benefits from using Pinia:
├── pages
├── static
└── stores
└── counter.js
├── App.vue
├── main.js
├── manifest.json
├── pages.json
└── uni.scss
Write the following code in main.js
:
import { createSSRApp } from 'vue';
import * as Pinia from 'pinia';
export function createApp() {
const app = createSSRApp(App);
app.use(Pinia.createPinia());
return {
app,
};
}
First create a store:
// stores/counter.js
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => {
return { count: 0 };
},
// state: () => ({ count: 0 })
actions: {
increment() {
this.count++;
},
},
});
and then use it in the component:
import { useCounterStore } from '@/stores/counter';
export default {
setup() {
const counter = useCounterStore();
counter.count++;
// can be triggered manually
counter.$patch({ count: counter.count + 1 });
// or use actions
counter.increment();
},
};
You can even use a function (similar to a component setup()) to define a Store for more advanced use cases:
export const useCounterStore = defineStore('counter', () => {
const count = ref(0);
function increment() {
count.value++;
}
return { count, increment };
});
If you're still unfamiliar with the setup() Composition API
, don't worry, Pinia also supports a set of Vuex-like map helpers
.
You define stores in the same way and then access them using mapStores()
, mapState()
or mapActions()
:
const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
getters: {
double: (state) => state.count * 2,
},
actions: {
increment() {
this.count++
}
}
})
const useUserStore = defineStore('user', {
// ...
})
export default {
computed: {
// ...
// Access via this.counterStore and this.userStore
...mapStores(useCounterStore, useUserStore)
// accessed via this.count and this.double
...mapState(useCounterStore, ['count', 'double']),
},
methods: {
// accessed via this.increment()
...mapActions(useCounterStore, ['increment']),
},
}