@fesjs/plugin-pinia

介绍

集成 pinia在新窗口打开 ,提供状态管理的能力,封装一些胶水代码,可以直接定义store 使用。

为了防止 Fes.jspinia 提供的 API 冲突,Fes.js不提供任何 pinia 的API,相关API直接从 pinia 导出:

import { defineStore } from 'pinia';
1

约定 plugin 定义放在 stores 目录下,文件名包含plugin被解析为插件,无需额外配置,定义即可用。

└── src
    ├── pages
    │    └── index.vue
    └── stores 
    │    ├── plugin-logger.js       
    │    ├── user.js
    └── app.js
1
2
3
4
5
6
7

启用方式

package.json 中引入依赖:

{
    "dependencies": {
        "@fesjs/fes": "^2.0.0",
        "@fesjs/plugin-pinia": "^2.0.0",
        "pinia": "^2.0.11"
    }
}
1
2
3
4
5
6
7

API

pinia

createPinia执行后创建的实例。

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

使用

定义 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...
})
1
2
3
4
5
6
7

setup

import { useStore } from '@/store/main'
export default {
    setup(){
        const store = useStore()
    }
}
1
2
3
4
5
6

非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);
        });
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18