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.jspinia 提供的 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"
    }
}

API

pinia

createPinia执行后创建的实例。

import { pinia } from '@fesjs/fes';

使用

定义 store

我们在 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...
});

setup

import { useStore } from '@/store/main';
export default {
    setup() {
        const store = useStore();
    },
};

非 setup

比如在 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);
        });
    },
};
`,20);function v(m,b){const a=t("ExternalLinkIcon");return p(),o("div",null,[r,u,n("p",null,[s("集成 "),n("a",d,[s("pinia"),i(a)]),s(" ,提供状态管理的能力,封装一些胶水代码,可以直接定义 store 使用。")]),k])}const g=e(l,[["render",v],["__file","pinia.html.vue"]]);export{g as default};