fes.js/docs/reference/plugin/plugins/pinia.md
2022-02-02 22:04:01 +08:00

90 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# @fesjs/plugin-pinia
## 介绍
集成 [pinia](https://pinia.vuejs.org/) 提供状态管理的能力封装一些胶水代码可以直接定义store 使用。
为了防止 `Fes.js``pinia` 提供的 API 冲突,`Fes.js`不提供任何 `pinia` 的API相关API直接从 `pinia` 导出:
```js
import { defineStore } from 'pinia';
```
约定 `plugin` 定义放在 `stores` 目录下文件名包含plugin被解析为插件无需额外配置定义即可用。
```
└── src
├── pages
│ └── index.vue
└── stores
│ ├── plugin-logger.js
│ ├── user.js
└── app.js
```
## 启用方式
`package.json` 中引入依赖:
```json
{
"dependencies": {
"@fesjs/fes": "^2.0.0",
"@fesjs/plugin-pinia": "^2.0.0",
"pinia": "^2.0.11"
}
}
```
## API
### pinia
`createPinia`执行后创建的实例。
```js
import { pinia } from '@fesjs/fes'
```
## 使用
### 定义 store
我们在 `src/store/main.js`中:
```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
```js
import { useStore } from '@/store/main'
export default {
setup(){
const store = useStore()
}
}
```
### 非setup
比如在app.js中:
```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);
});
}
};
```