Files
testing-tool/sidepanel/modules/Router.js
T
雨霖铃 28fc3b409f feat(router): 重构路由模块并优化页面加载逻辑
- 引入 ScriptManager、TimerManager 和 EventManager 统一管理资源
- 路由配置支持默认重定向路径 '/'
- 移除旧版 scriptLoader 与 routeUtils,使用新的模块化加载机制
- 增加路由生命周期控制,自动清理定时器和事件监听器
- 改进点击代理逻辑,增强按钮激活状态切换
- 页面切换时卸载旧脚本及资源,防止内存泄漏
- 修复初始化日志冗余问题,提升代码可维护性
2025-12-09 22:22:31 +08:00

136 lines
3.1 KiB
JavaScript

import { TimerManager } from '../utils/timerManager.js';
import { EventManager } from '../utils/eventManager.js';
import { ScriptManager } from '../utils/scriptManager.js';
class Router {
constructor() {
this.routes = {};
this.beforeFun = null;
this.afterFun = null;
this.routerViewId = 'app';
this.redirectRoute = null;
this.stackPages = true;
this.routerMap = [];
this.historyFlag = '';
this.history = [];
this.scriptManager = window.scriptManager;
this.eventManager = window.eventManager;
this.timerManager = window.timerManager;
}
/**
* 初始化路由
*/
init(config) {
// 配置路由
this.routerMap = config?.routes || this.routerMap;
this.routerViewId = config?.routerViewId || this.routerViewId;
this.stackPages = config?.stackPages ?? this.stackPages;
this.map();
// 监听路由变化
window.addEventListener('hashchange', () => this.urlChange());
window.addEventListener('load', () => this.urlChange());
window.lintTo = (path) => this.naviage(path);
}
map() {
if (!this.routerMap.length) {
console.error('请配置路由');
return;
}
for (const r of this.routerMap) {
if (r.name == 'redirect') this.redirectRoute = r.path;
this.routes[r.path] = r;
}
}
/**
* 导航
*/
naviage(path) {
window.location.hash = path;
}
/**
* 路由解析与渲染
*/
urlChange() {
const path = location.hash.replace('#', '') || this.redirectRoute;
console.log('当前路由:', path);
const route = this.routes[path];
if (!route) {
location.hash = this.redirectRoute;
return;
}
const doChange = async () => {
this.timerManager.cleanAll();
this.eventManager.removeAll();
const mount = document.getElementById(this.routerViewId);
if (!mount) {
console.error('挂载点不存在:', this.routerViewId);
return;
}
if (!this.stackPages) mount.innerHTML = '';
if (route.html) {
try {
const html = await fetch(route.html).then((r) => r.text());
mount.innerHTML = html;
} catch (e) {
mount.innerHTML = `<h2>页面加载失败:${e.message}</h2>`;
}
}
if (route.script) {
await this.scriptManager.loadScript({
path: route.script,
name: route.name,
isModule: route.isModule,
deps: route.deps,
});
}
await this.scriptManager.runInit(route.name);
this.currentRoute = route;
if (this.afterFun) this.afterFun(route);
};
if (this.beforeFun) {
this.beforeFun({ to: route, next: doChange });
} else {
doChange();
}
}
/**
* before 钩子
*/
beforeEach(callback) {
if (typeof callback === 'function') {
this.beforeFun = callback;
} else {
console.trace('beforeEach 必须是函数');
}
}
/**
* after 钩子
*/
afterEach(callback) {
if (typeof callback === 'function') {
this.afterFun = callback;
} else {
console.trace('afterEach 必须是函数');
}
}
}
export { Router };