feat(router): 重构路由模块并优化页面加载逻辑

- 引入 ScriptManager、TimerManager 和 EventManager 统一管理资源
- 路由配置支持默认重定向路径 '/'
- 移除旧版 scriptLoader 与 routeUtils,使用新的模块化加载机制
- 增加路由生命周期控制,自动清理定时器和事件监听器
- 改进点击代理逻辑,增强按钮激活状态切换
- 页面切换时卸载旧脚本及资源,防止内存泄漏
- 修复初始化日志冗余问题,提升代码可维护性
This commit is contained in:
雨霖铃
2025-12-09 22:22:31 +08:00
parent 2761dbe13f
commit 28fc3b409f
8 changed files with 282 additions and 199 deletions
+19 -12
View File
@@ -1,10 +1,17 @@
console.log('Before creating router');
import { ScriptManager } from '../utils/scriptManager.js';
import { TimerManager } from '../utils/timerManager.js';
import { EventManager } from '../utils/eventManager.js';
import { Router } from '../modules/Router.js';
const config = {
routerViewId: 'app',
stackPages: false,
routes: [
{
path: '/',
name: 'redirect',
html: 'pages/timestamp.html',
},
{
path: '/home',
name: 'timestamp',
@@ -15,23 +22,23 @@ const config = {
path: '/todo',
name: 'todo',
html: 'pages/todolist.html',
script: '',
},
],
};
window.scriptManager = new ScriptManager();
window.timerManager = new TimerManager();
window.eventManager = new EventManager();
const router = new Router();
console.log('Router created:', router);
router.init(config);
console.log('Router initialized');
document.addEventListener('click', (event) => {
const btn = event.target.closest('[data-route]');
// click delegation
document.addEventListener('click', (e) => {
const btn = e.target.closest('[data-route]');
if (!btn) return;
const path = btn.getAttribute('data-route');
if (!path) return;
// 触发 hash 路由跳转
window.location.hash = path;
const p = btn.getAttribute('data-route');
location.hash = p;
// update active styling
document.querySelectorAll('[data-route]').forEach((b) => b.classList.toggle('active', b === btn));
});
-1
View File
@@ -14,7 +14,6 @@ import { addEventListenerById } from '../utils/domUtils.js';
(function (global) {
global.timestampInit = async function () {
console.log('timestamp init');
// 初始化时间戳显示
initTimestampDisplay();