跳到主要内容
版本:2.1.0

使用模块

除了被主动加载的文件(插件入口点),其他 Js 文件只能通过作为模块被读取和加载

编写模块

使用 export 导出变量、函数、类

Serein/plugins/test/myModule.js
import { test } from './anotherLib.js'

export const value = 114514;

export async function getPosts() {
// ...
}

export class Student {
// ...
}

模块中可使用import ... from '...'导入其他模块

Serein/plugins/test/myModule.js
import { test } from './anotherLib.js'

导入

在被加载的 JS 文件中,你可以通过 require() 函数导入其他模块中导出的内容

require(file: string): any
参数类型说明
filestring相对于被加载文件的路径
Serein/plugins/test/index.js

const { value, getPosts, Student } = require('./myModule.js')