升级你的生命周期事件函数


    1. 函数形式(已经作废,仅为兼容保留)。
    2. 类形式(推荐使用)。

    我们通常在 app.js 中通过 中传入的 app 参数进行此函数的操作,一个典型的例子:

    1. // app.js 或 agent.js 文件:
    2. class AppBootHook {
    3. constructor(app) {
    4. this.app = app;
    5. }
    6. async didLoad() {
    7. // 请将你的插件项目中 app.beforeStart 中的代码置于此处。
    8. // 请将你的应用项目中 app.beforeStart 中的代码置于此处。
    9. }
    10. }
    11. module.exports = AppBootHook;

    同样地,我们之前在 app.ready 中处理我们的逻辑:

    1. // app.js 或 agent.js 文件:
    2. class AppBootHook {
    3. constructor(app) {
    4. this.app = app;
    5. }
    6. async didReady() {
    7. }
    8. }
    9. module.exports = AppBootHook;

    原先的 app.beforeClose 如以下形式:

    1. // app.js 或 agent.js 文件:
    2. class AppBootHook {
    3. constructor(app) {
    4. this.app = app;
    5. }
    6. async beforeClose() {
    7. // 请将您的 app.beforeClose 中的代码置于此处。
    8. }

    本教程只是一对一地讲了替换方法,便于开发者们快速上手进行替换;若想要具体了解整个 Loader 原理以及生命周期的完整函数版本,请参考加载器和两篇文章。