796e73ae5a
* refactor(doc): change to new structure * docs: add database docs * docs: add collection docs * docs: add db field examples * docs(api): fix filename and menu path * docs: add database docs * docs: add db operators doc * docs: add resourcer menu * docs: add resourcer docs * docs: fix api docs * docs: refactor api menu structure * feat: update docs (#830) * feat: updates * feat: update docs * chore: ignore docs from ci Co-authored-by: Junyi <mytharcher@users.noreply.github.com> Co-authored-by: mytharcher <mytharcher@gmail.com> * docs: add database methods docs * docs: add missed api * docs: fix api docs * feat: update development docs (#833) * feat: update development docs * feat: update docs * feat: update docs * docs: add first plugin example (#834) * feat: update docs * feat: update docs * docs: fix typo Co-authored-by: chenos <chenlinxh@gmail.com>
97 lines
1.6 KiB
Markdown
97 lines
1.6 KiB
Markdown
# 编写第一个插件
|
||
|
||
在此之前,需要先安装好 NocoBase:
|
||
|
||
- create-nocobase-app 安装
|
||
- Git 源码安装
|
||
|
||
相关安装、启动命令如下:
|
||
|
||
```bash
|
||
cd my-nocobase-app
|
||
yarn install
|
||
yarn nocobase install
|
||
yarn dev
|
||
```
|
||
|
||
插件开发和调试,需要在开发环境并通过 `yarn dev` 启动。做好准备之后,我们就可以开始 NocoBase 的插件开发之旅了。
|
||
|
||
## 创建插件
|
||
|
||
首先,你可以通过 CLI 快速的创建一个初始化的插件,命令如下:
|
||
|
||
```bash
|
||
yarn pm create hello
|
||
```
|
||
|
||
新建的插件,会放置在 `packages/plugins/hello` 目录下。
|
||
|
||
## 插件目录结构
|
||
|
||
```ts
|
||
|- /hello
|
||
|- /src
|
||
|- /client
|
||
|- /server
|
||
|- client.d.ts
|
||
|- client.js
|
||
|- package.json
|
||
|- server.d.ts
|
||
|- server.js
|
||
```
|
||
|
||
## 编写插件
|
||
|
||
插件的主体文件在 `packages/plugins/hello/src/server/plugin.ts`,修改为:
|
||
|
||
```ts
|
||
import { InstallOptions, Plugin } from '@nocobase/server';
|
||
|
||
export class Hello extends Plugin {
|
||
initialize() {
|
||
// TODO
|
||
}
|
||
|
||
beforeLoad() {
|
||
// TODO
|
||
}
|
||
|
||
async load() {
|
||
// TODO
|
||
// Visit: http://localhost:13000/api/hello:get
|
||
this.app.resource({
|
||
name: 'hello',
|
||
actions: {
|
||
async get(ctx, next) {
|
||
ctx.body = `Hello plugin1!`;
|
||
next();
|
||
},
|
||
},
|
||
});
|
||
this.app.acl.allow('hello', 'get');
|
||
}
|
||
|
||
async install(options: InstallOptions) {
|
||
// TODO
|
||
}
|
||
}
|
||
|
||
export default Hello;
|
||
```
|
||
|
||
## 注册插件
|
||
|
||
```bash
|
||
yarn pm add hello
|
||
```
|
||
|
||
## 激活插件
|
||
|
||
```bash
|
||
yarn pm enable hello
|
||
```
|
||
|
||
## 体验插件功能
|
||
|
||
访问地址 http://localhost:13000/api/hello:get
|