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;
},
},
});
<script setup>
import { useStore } from './store/index'
const counter = useStore()
const counterAdd = (num)=>{
counter.add(num)
}
</script>
<template>
<div>
{{counter.a}}
{{counter.double}}
<button @click="counter.a++">直接累加</button>
<button @click="counterAdd(1)">点击</button>
</div>
</template>
<style scoped>
</style>
使用自己的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;
},
},
});
- 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);
}
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
}
实现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
},{})
);
}
实现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);
}
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
}
|