升级你的生命周期事件函数
- 函数形式(已经作废,仅为兼容保留)。
- 类形式(推荐使用)。
我们通常在 app.js 中通过 中传入的 app
参数进行此函数的操作,一个典型的例子:
// app.js 或 agent.js 文件:
class AppBootHook {
constructor(app) {
this.app = app;
}
async didLoad() {
// 请将你的插件项目中 app.beforeStart 中的代码置于此处。
// 请将你的应用项目中 app.beforeStart 中的代码置于此处。
}
}
module.exports = AppBootHook;
同样地,我们之前在 app.ready
中处理我们的逻辑:
// app.js 或 agent.js 文件:
class AppBootHook {
constructor(app) {
this.app = app;
}
async didReady() {
}
}
module.exports = AppBootHook;
原先的 app.beforeClose
如以下形式:
// app.js 或 agent.js 文件:
class AppBootHook {
constructor(app) {
this.app = app;
}
async beforeClose() {
// 请将您的 app.beforeClose 中的代码置于此处。
}
本教程只是一对一地讲了替换方法,便于开发者们快速上手进行替换;若想要具体了解整个 Loader 原理以及生命周期的完整函数版本,请参考加载器和两篇文章。