Files
testing-tool/sidepanel/js/components/BaseComponet.js
T
雨霖铃 8cb9580860 refactor(router): 优化路由重定向配置与组件初始化逻辑
- 设置默认重定向路由为 '/'
- 修复路由实例化后未正确调用 init 方法的问题
- 移除不必要的控制台日志输出
- 调整路由守卫中 next 回调的参数传递方式
- 导出 Router 类时增加空格以符合代码风格
- 更新时间戳组件导入方式并移除冗余初始化调用
- 为定时器管理器添加清理功能的日志提示
- 从 BaseComponent 中移除调试日志并修复定时器返回值处理
2025-12-13 21:52:52 +08:00

69 lines
1.7 KiB
JavaScript

export class BaseComponent {
constructor() {
this._timerManager = window.timerManager;
this._scriptManager = window.scriptManager;
this._eventManager = window.eventManager;
}
/**
* 安全绑定事件,并自动记录以记录销毁
* @param {string | HTMLElement} target 目标元素
* @param {string} type 事件类型
* @param {Function} handler 回调函数
*/
bindEvent(target, type, handler) {
// 获取目标元素
const ele = typeof target === 'string' ? document.getElementById(target) : target;
if (!ele) {
console.error(`Element with ID ${target} not found.`);
return;
}
ele.addEventListener(type, handler);
this._eventManager.add(ele, type, handler);
}
/**
* 创建定时器
* @param {Function} fn 定时器回调函数
* @param {number} ms 定时器间隔时间(毫秒)
*/
setInterval(fn, ms) {
return this._timerManager.setInterval(fn, ms);
}
/**
* 创建定时器
* @param {Function} fn 定时器回调函数
* @param {number} ms 定时器间隔时间(毫秒)
*/
setTimeout(fn, ms) {
return this._timerManager.setTimeout(fn, ms);
}
/**
* 加载脚本
* @param {string} path 脚本路径
* @param {string} name 脚本名称
* @param {boolean} isModule 是否为模块
* @param {string[]} deps 依赖的脚本
*/
loadScript(path, name, isModule = false, deps = []) {
this._scriptManager.loadScript({path, name, isModule, deps});
}
/**
* 销毁组件
*/
destroy() {
this._eventManager.removeAll();
this._timerManager.cleanAll();
this._scriptManager.unloadScript().then(r => console.log(r));
console.log('destroy success');
}
}
window.BaseComponent = BaseComponent;