2761dbe13f
- 移除冗余的开发预览页面 `index.html` - 路由配置改为声明式结构,支持自动加载 HTML 和脚本 - 新增 `data-route` 属性用于导航按钮绑定路由路径 - 实现基于事件委托的路由跳转机制 - 重写 `Router.js` 核心逻辑,支持异步加载页面模块 - 改进 `domUtils.js` 中 `addClass` 和 `removeClass` 方法兼容性与健壮性 - 新增 `scriptLoader.js` 工具模块用于动态加载和清理页面脚本 - 时间戳页面初始化方式调整为 IIFE 并挂载至全局作用域
38 lines
790 B
JavaScript
38 lines
790 B
JavaScript
console.log('Before creating router');
|
|
import { Router } from '../modules/Router.js';
|
|
|
|
const config = {
|
|
routerViewId: 'app',
|
|
stackPages: false,
|
|
routes: [
|
|
{
|
|
path: '/home',
|
|
name: 'timestamp',
|
|
html: 'pages/timestamp.html',
|
|
script: '../dist/timestamp.js',
|
|
},
|
|
{
|
|
path: '/todo',
|
|
name: 'todo',
|
|
html: 'pages/todolist.html',
|
|
script: '',
|
|
},
|
|
],
|
|
};
|
|
|
|
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]');
|
|
if (!btn) return;
|
|
|
|
const path = btn.getAttribute('data-route');
|
|
if (!path) return;
|
|
|
|
// 触发 hash 路由跳转
|
|
window.location.hash = path;
|
|
});
|