Simple use of Vue state management tool pinia
1 Introduction
Pinia
is a Vue
state management tool, which is Vue 3
one of the officially recommended state management libraries. Pinia
The goal of is to provide a simple, lightweight state management solution, which is Vue 3
built on top of the new reactive API and the new composition API, which is very intuitive and natural to use.
2. The use of pinia in vue3
- First install it in the project
pinia
, execute it , and then introduce it innpm install pinia
the entry filemain.js
- We choose
Vue
a similar approach to the optional API of , passing in an object with thestate
,actions
andgetters
propertiesOption
- You can think of
state
isstore
the data of the(data)
,getters
isstore
the computed property of the(computed)
, andactions
is the method(methods)
.
// main.js
import {
createApp } from 'vue'
import {
createPinia } from "pinia"
import App from './App.vue'
import './assets/main.css'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.mount('#app')
- Create a store
// src/store/user.js
import {
defineStore } from "pinia"
export const useUserStore = defineStore({
id: "user"
})
- Define the state and introduce the use
// src/store/user.js
import {
defineStore } from "pinia"
export const useUserStore = defineStore({
id: "user",
state: () => {
return {
firstName: "Jack",
lastName: "Joe",
age: 18
}
}
})
// src/pages/user.vue
<template>
<div class="user">
{
{ user.firstName }} -- {
{ user.lastName }}
</div>
</template>
<script setup>
import {
useUserStore } from "../store/user"
const user = useUserStore()
</script>
- Define the getter and introduce the use
// src/store/user.js
import {
defineStore } from "pinia"
export const useUserStore = defineStore({
id: "user",
state: () => {
return {
firstName: "Jack",
lastName: "Joe",
age: 18
}
},
getters: {
fullName(state) {
return state.firstName + " " + state.lastName
}
}
})
// src/pages/user.vue
<template>
<div class="user">
{
{ user.fullName }}
</div>
</template>
<script setup>
import {
useUserStore } from "../store/user"
const user = useUserStore()
console.log(user.fullName);
</script>
- Define the action and use it. The methods in the action can be synchronous or asynchronous.
// src/store/user.js
import {
defineStore } from "pinia"
function apiGetData(name) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(name === "Jack Joe") {
resolve({
age: 18 })
} else {
reject({
msg: "查无此人!" })
}
}, 2000);
})
}
export const useUserStore = defineStore({
id: "user",
state: () => {
return {
firstName: "Jack",
lastName: "Joe",
age: 18
}
},
actions: {
changeName(name) {
[ this.firstName, this.lastName ] = name.split(" ")
},
async getUserData(name) {
try {
const res = await apiGetData(name)
return res
} catch(err) {
return err
}
}
},
getters: {
fullName(state) {
return state.firstName + " " + state.lastName
}
}
})
// src/pages/user.vue
<template>
<div class="user">
{
{ user.fullName }}
</div>
</template>
<script setup>
import {
} from "vue"
import {
useUserStore } from "../store/user"
const user = useUserStore()
console.log(user.fullName);
//同步方法
user.changeName("Marry Gren")
//异步方法
const getUser = async () => {
const userData = await user.getUserData("Jack Joe")
console.log(userData);// {age: 18}
}
getUser()
</script>