Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成作业 #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ <h1>Welcome</h1>
title: '首页'
}, {
id: 'next',
title: '下一页',
handler: function() {
console.log("切换到next");
console.log(this);
}
title: '下一页'
}]

//路由器
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"http-server": "^14.1.1",
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.1.2"
}
Expand Down
56 changes: 43 additions & 13 deletions router.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
function Router(routes, defaultRoute) {
//TODO
this.init(routes, defaultRoute);
}

//初始化,可多次初始化
Router.prototype.init = function(routes, defaultRoute) {
Router.prototype.init = function (routes, defaultRoute) {
//TODO
this.currentRoute
this.oldRoute
}
this.routes = routes;
this.routeMap = {};
routes.forEach((currenValue) => {
this.routeMap[currenValue.id] = currenValue;
});
this.stack = [];
this.stackIndex = -1;
this.push(defaultRoute);
};

//切换路由
Router.prototype.push = function(route, callback) {
//TODO
}
Router.prototype.push = function (route, callback) {
this.changePage(route);
history.pushState(this.currentRoute, this.currentRoute.title);
let res;
if (callback) {
try {
res = callback(this.currentRoute, this);
} catch (error) {
res = undefined;
}
}
return res;
};

Router.prototype.replace = function (route) {
this.changePage(route);
history.replaceState(this.currentRoute, this.currentRoute.title);
}
//前进
Router.prototype.forward = function () {
history.forward();
}
this.stackIndex++;
this.currentRoute = this.stack[this.stackIndex];
};

//后退
Router.prototype.back = function () {
history.back();
}
this.stackIndex--;
this.currentRoute = this.stack[this.stackIndex];
};

//切换页面:route为字符串,为空则隐藏所有路由
Router.prototype.changePage = function(route) {
//TODO
}
Router.prototype.changePage = function (route) {
// TODO
const currentRoute = this.routeMap[route];
this.currentRoute = currentRoute;
this.stack.push(currentRoute);
this.stackIndex++;
};


module.exports = Router
module.exports = Router;
2 changes: 2 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Router = require('../router');

describe('初始化路由', () => {
it('default route', () => {
//路由表
Expand Down