前言 全局狀態(tài)管理工具
Pinia.js 有如下特點:
- 完整的 ts 的支持;
- 足夠輕量,壓縮后的體積只有1kb左右;
- 去除 mutations,只有 state,getters,actions;
- actions 支持同步和異步;
- 代碼扁平化沒有模塊嵌套,只有 store 的概念,store 之間可以自由使用,每一個store都是獨立的
- 無需手動添加 store,store 一旦創(chuàng)建便會自動添加;
- 支持Vue3 和 Vue2
1.起步 安裝
yarn add pinia
npm install pinia
2.引入注冊Vue3
import { createApp } from 'vue'
import App from './App.vue'
import {createPinia} from 'pinia'
const store = createPinia()
let app = createApp(App)
app.use(store)
app.mount('#app')
Vue2 使用
import { createPinia, PiniaVuePlugin } from 'pinia'
Vue.use(PiniaVuePlugin)
const pinia = createPinia()
new Vue({
el: '#app',
// other options...
// ...
// note the same `pinia` instance can be used across multiple Vue apps on
// the same page
pinia,
})