跳转到内容

基础教程

本教程将带你了解项目的基础功能,适合初学者。

完成本教程后,你将能够:

  • 创建和配置应用实例
  • 理解基本的生命周期
  • 处理常见的使用场景

首先,让我们创建一个基本的应用:

import { createApp } from 'your-package-name';
const app = createApp({
name: 'Tutorial App',
debug: true
});

你可以自定义各种选项:

const app = createApp({
name: 'Tutorial App',
version: '1.0.0',
debug: true,
timeout: 10000
});
  • name - 应用名称,用于日志和识别
  • version - 版本号,可选
  • debug - 启用调试输出
  • timeout - 操作超时时间(毫秒)

启动应用非常简单:

await app.run();
console.log('应用已启动!');

当需要停止时:

await app.stop();
console.log('应用已停止');

将所有步骤组合在一起:

import { createApp } from 'your-package-name';
async function main() {
const app = createApp({
name: 'Tutorial App',
debug: true
});
try {
await app.run();
console.log('应用运行中...');
// 执行一些操作
await new Promise(resolve => setTimeout(resolve, 5000));
await app.stop();
console.log('应用已停止');
} catch (error) {
console.error('错误:', error);
}
}
main();

尝试以下练习来巩固学习:

  1. 创建一个应用,设置 30 秒超时
  2. 启用调试模式并观察输出
  3. 添加错误处理逻辑