pinia原理
创建项目
- 新建项目vite-pinia
- 创建vite项目
- 安装pinia
mkdir vite-pinia
cd vite-pinia
yarn create vite
yarn add pinia
code .
使用pinia
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createPinia } from 'pinia'
const app = createApp(App)
app.use(createPinia()).mount('#app')
import { defineStore } from "pinia";
export const useStore = defineStore({
id: "main",
state: () => {
return {
a: 1,
};
},
getters: {
double: (store) => {
return store.a * 2;
},
},
actions: {
add(num) {
this.a += 1;
},
},
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
{{counter.a}}
{{counter.double}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
使用自己的pinia
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
export default defineConfig({
plugins: [vue()],
resolve:{
alias: {
'@': path.resolve(__dirname, './src'),
}
}
})
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from '@/pinia'
const app = createApp(App)
app.use(createPinia()).mount('#app')
import { defineStore } from "@/pinia";
export const useStore = defineStore({
id: "main",
state: () => {
return {
a: 1,
};
},
getters: {
double: (store) => {
return store.a * 2;
},
},
actions: {
add(num) {
this.a += 1;
},
},
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- src下新增pinia文件夹,添加index.js
实现pinia的store
- pinia/index.js
- 导出两个方法,defineStore定义store,createPinia创建一个pinia
import { defineStore } from "./defineStore";
import { createPinia } from "./createPinia";
export {
defineStore,
createPinia
}
import { effectScope, getCurrentInstance, inject, reactive } from "vue";
import { SymbolPinia } from "./rootStore";
export function defineStore(idOrOptions, setup) {
let id;
let options;
if (typeof idOrOptions === "string") {
id = idOrOptions;
options = setup;
} else {
options = idOrOptions;
id = idOrOptions.id;
}
function useStore() {
const currentInstance = getCurrentInstance();
const pinia = currentInstance && inject(SymbolPinia);
if (!pinia._s.has(id)) {
createOptionsStore(id, options, pinia);
}
const store = pinia._s.get(id);
return store;
}
return useStore;
}
function createOptionsStore(id, options, pinia) {
let { state, getters, actions } = options;
let scope;
const store = reactive({});
function setup() {
pinia.state.value[id] = state ? state() : {};
const localState = pinia.state.value[id];
return localState;
}
const setupStore = pinia._e.run(() => {
scope = effectScope();
return scope.run(() => setup());
});
Object.assign(store, setupStore);
pinia._s.set(id, store);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
import { effectScope, markRaw, ref } from "vue";
import { SymbolPinia } from "./rootStore";
export function createPinia() {
const scope = effectScope(true);
const state = scope.run(() => ref({}));
const pinia = markRaw({
install(app) {
pinia._a = app;
app.provide(SymbolPinia, pinia);
app.config.globalProperties.$pinia = pinia;
},
_a: null,
state,
_e: scope,
_s: new Map(),
});
return pinia
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
实现pinia的getters
function setup() {
pinia.state.value[id] = state ? state() : {};
const localState = toRefs(pinia.state.value[id]);
return Object.assign(
localState,
actions,
Object.keys(getters || {}).reduce((computedCetters, name) => {
computedCetters[name] = computed(() => {
return getters[name].call(store,store)
});
return computedCetters
},{})
);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
实现pinia的actions
function createOptionsStore(id, options, pinia) {
let { state, getters, actions } = options;
let scope;
const store = reactive({});
function setup() {
pinia.state.value[id] = state ? state() : {};
const localState = pinia.state.value[id];
return Object.assign(localState, actions);
}
const setupStore = pinia._e.run(() => {
scope = effectScope();
return scope.run(() => setup());
});
function warpAction(name, action) {
return function () {
let ret = action.apply(store, arguments);
return ret;
};
}
for (let key in setupStore) {
const prop = setupStore[key];
if (typeof prop === "function") {
setupStore[key] = warpAction(key, prop);
}
}
Object.assign(store, setupStore);
pinia._s.set(id, store);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
pinia的参数是函数的时候
import { defineStore } from "@/pinia";
import { computed, reactive, toRefs } from "vue";
export const useStore = defineStore('mian',()=>{
const state = reactive({a:1})
const double = computed(()=>{
return state.a*2
})
const add = (num)=> state.a +=num
return { ...toRefs(state),double,add }
})
import {
computed,
effectScope,
getCurrentInstance,
inject,
reactive,
toRefs,
} from "vue";
import { SymbolPinia } from "./rootStore";
export function defineStore(idOrOptions, setup) {
let id;
let options;
if (typeof idOrOptions === "string") {
id = idOrOptions;
options = setup;
} else {
options = idOrOptions;
id = idOrOptions.id;
}
const isSetupStore = typeof setup === "function";
function useStore() {
const currentInstance = getCurrentInstance();
const pinia = currentInstance && inject(SymbolPinia);
if (!pinia._s.has(id)) {
if (isSetupStore) {
createSetupStore(id, setup, pinia);
} else {
createOptionsStore(id, options, pinia);
}
}
const store = pinia._s.get(id);
return store;
}
return useStore;
}
function createSetupStore(id,setup,pinia){
const store = reactive({});
let scope;
const setupStore = pinia._e.run(() => {
scope = effectScope();
return scope.run(() => setup());
});
function warpAction(name, action) {
return function () {
let ret = action.apply(store, arguments);
return ret;
};
}
for (let key in setupStore) {
const prop = setupStore[key];
if (typeof prop === "function") {
setupStore[key] = warpAction(key, prop);
}
}
Object.assign(store, setupStore);
pinia._s.set(id, store);
return store
}
function createOptionsStore(id, options, pinia) {
let { state, getters, actions } = options;
function setup() {
pinia.state.value[id] = state ? state() : {};
const localState = toRefs(pinia.state.value[id]);
return Object.assign(
localState,
actions,
Object.keys(getters || {}).reduce((computedCetters, name) => {
computedCetters[name] = computed(() => {
return getters[name].call(store, store);
});
return computedCetters;
}, {})
);
}
const store = createSetupStore(id,setup,pinia)
return store
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100