Skip to main content

函数

¥Functions

./src/index.js 文件(或 基于 TypeScript 项目中的 ./src/index.ts 文件)包括全局 registerbootstrapdestroy 函数,可用于添加动态和基于逻辑的配置。

¥The ./src/index.js file (or ./src/index.ts file in a TypeScript-based project) includes global register, bootstrap and destroy functions that can be used to add dynamic and logic-based configurations.

这些函数可以是同步的、异步的或返回一个 promise。

¥The functions can be synchronous, asynchronous, or return a promise.

Lifecycle functions diagramLifecycle functions diagram

同步函数

¥Synchronous function

module.exports = {
register() {
// some sync code
},
bootstrap() {
// some sync code
},
destroy() {
// some sync code
}
};

异步函数

¥Asynchronous function

module.exports = {
async register() {
// some async code
},
async bootstrap() {
// some async code
},
async destroy() {
// some async code
}
};

返回 promise 的函数

¥Function returning a promise

module.exports = {
register() {
return new Promise(/* some code */);
},
bootstrap() {
return new Promise(/* some code */);
},
destroy() {
return new Promise(/* some code */);
}
};

注册

¥Register

register 生命周期函数可在 ./src/index.js(或 ./src/index.ts)中找到,是一个在应用初始化之前运行的异步函数。它可用于:

¥The register lifecycle function, found in ./src/index.js (or in ./src/index.ts), is an asynchronous function that runs before the application is initialized. It can be used to:

register() 是 Strapi 应用启动时发生的第一件事。这发生在任何设置过程之前,并且你无权访问 register() 函数中的数据库、路由、策略或任何其他后端服务器元素。

¥register() is the very first thing that happens when a Strapi application is starting. This happens before any setup process and you don't have any access to database, routes, policies, or any other backend server elements within the register() function.

引导程序

¥Bootstrap

./src/index.js(或 ./src/index.ts)中找到的 bootstrap 生命周期函数在每次服务器启动时都会被调用。

¥The bootstrap lifecycle function, found in ./src/index.js (or in ./src/index.ts), is called at every server start.

它可用于:

¥It can be used to:

bootstrapi() 函数在后端服务器启动之前、Strapi 应用设置之后运行,因此你可以访问 strapi 对象中的任何内容。

¥The bootstrapi() function is run before the back-end server starts but after the Strapi application has setup, so you have access to anything from the strapi object.

提示

你可以在终端中运行 yarn strapi console(或 npm run strapi console)并与 strapi 对象进行交互。

¥You can run yarn strapi console (or npm run strapi console) in the terminal and interact with the strapi object.

销毁

¥Destroy

destroy 函数位于 ./src/index.js(或 ./src/index.ts)中,是一个异步函数,在应用关闭之前运行。

¥The destroy function, found in ./src/index.js (or in ./src/index.ts), is an asynchronous function that runs before the application gets shut down.

它可以优雅地用于:

¥It can be used to gracefully: