import{_ as e,r as t,o as p,c as o,b as n,d as s,a as i,f as c}from"./app.0e52fd78.js";const l={},r=n("h1",{id:"fesjs-plugin-pinia",tabindex:"-1"},[n("a",{class:"header-anchor",href:"#fesjs-plugin-pinia","aria-hidden":"true"},"#"),s(" @fesjs/plugin-pinia")],-1),u=n("h2",{id:"介绍",tabindex:"-1"},[n("a",{class:"header-anchor",href:"#介绍","aria-hidden":"true"},"#"),s(" 介绍")],-1),d={href:"https://pinia.vuejs.org/",target:"_blank",rel:"noopener noreferrer"},k=c(`
为了防止 Fes.js
与 pinia
提供的 API 冲突,Fes.js
不提供任何 pinia
的 API,相关 API 直接从 pinia
导出:
import { defineStore } from 'pinia';
约定 plugin
定义放在 stores
目录下,文件名包含 plugin 被解析为插件,无需额外配置,定义即可用。
└── src
├── pages
│ └── index.vue
└── stores
│ ├── plugin-logger.js
│ ├── user.js
└── app.js
在 package.json
中引入依赖:
{
"dependencies": {
"@fesjs/fes": "^3.0.0",
"@fesjs/plugin-pinia": "^3.0.0",
"pinia": "^2.0.11"
}
}
createPinia
执行后创建的实例。
import { pinia } from '@fesjs/fes';
我们在 src/store/main.js
中:
import { defineStore } from 'pinia';
// useStore could be anything like useUser, useCart
// the first argument is a unique id of the store across your application
export const useStore = defineStore('main', {
// other options...
});
import { useStore } from '@/store/main';
export default {
setup() {
const store = useStore();
},
};
比如在 app.js 中:
import { pinia } from '@fesjs/fes';
export const beforeRender = {
loading: <PageLoading />,
action() {
const { setRole } = accessApi;
return new Promise((resolve) => {
setTimeout(() => {
const store = useStore(pinia);
store.$patch({
userName: '李雷',
role: 'admin',
});
setRole('admin');
}, 1000);
});
},
};