From 96169327423a694a6188aa941c4511d456ac6e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E9=98=B3=E9=B9=8F?= <61752998+iuroc@users.noreply.github.com> Date: Tue, 9 Jan 2024 05:31:43 +0000 Subject: [PATCH] version 1.2.2 --- README.md | 2 +- js/index.js | 16 ++++++++-------- ...js-router-1.2.1.js => vanjs-router-1.2.2.js} | 17 +++++++++-------- package.json | 2 +- src/index.ts | 16 ++++++++-------- 5 files changed, 27 insertions(+), 26 deletions(-) rename js/{vanjs-router-1.2.1.js => vanjs-router-1.2.2.js} (65%) diff --git a/README.md b/README.md index 334d7b4..8a54bb1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ npm install vanjs-core vanjs-router ```html - + ``` ### Import and Usage diff --git a/js/index.js b/js/index.js index 65d9bd9..61302bd 100644 --- a/js/index.js +++ b/js/index.js @@ -10,20 +10,20 @@ const activeRoute = van.state(nowRoute()); window.addEventListener('hashchange', () => { activeRoute.val = nowRoute(); }); -const Route = (config, ...rest) => { - const { name, onFirst, onLoad, ...otherProp } = config; +const Route = (first, ...rest) => { + const { name, onFirst, onLoad, ...otherProp } = first; let firstLoad = true; van.derive(() => { - if (activeRoute.val.name == config.name) { - if (firstLoad && config.onFirst) { - config.onFirst(activeRoute.val); + if (activeRoute.val.name == first.name) { + if (firstLoad && first.onFirst) { + first.onFirst(activeRoute.val); firstLoad = false; } - if (config.onLoad) - config.onLoad(activeRoute.val); + if (first.onLoad) + first.onLoad(activeRoute.val); } }); - return div({ hidden: () => config.name != activeRoute.val.name, ...otherProp }, rest); + return div({ hidden: () => first.name != activeRoute.val.name, ...otherProp }, rest); }; const routeTo = (name = 'home', args = []) => { if (args.length == 0) { diff --git a/js/vanjs-router-1.2.1.js b/js/vanjs-router-1.2.2.js similarity index 65% rename from js/vanjs-router-1.2.1.js rename to js/vanjs-router-1.2.2.js index 7ed537a..baa422f 100644 --- a/js/vanjs-router-1.2.1.js +++ b/js/vanjs-router-1.2.2.js @@ -9,19 +9,20 @@ const activeRoute = van.state(nowRoute()); window.addEventListener('hashchange', () => { activeRoute.val = nowRoute(); }); -const Route = (config, ...rest) => { +const Route = (first, ...rest) => { + const { name, onFirst, onLoad, ...otherProp } = first; let firstLoad = true; van.derive(() => { - if (activeRoute.val.name == config.name) { - if (firstLoad && config.onFirst) { - config.onFirst(activeRoute.val); + if (activeRoute.val.name == first.name) { + if (firstLoad && first.onFirst) { + first.onFirst(activeRoute.val); firstLoad = false; } - if (config.onLoad) - config.onLoad(activeRoute.val); + if (first.onLoad) + first.onLoad(activeRoute.val); } }); - return div({ hidden: () => config.name != activeRoute.val.name }, rest); + return div({ hidden: () => first.name != activeRoute.val.name, ...otherProp }, rest); }; const routeTo = (name = 'home', args = []) => { if (args.length == 0) { @@ -36,4 +37,4 @@ const routeTo = (name = 'home', args = []) => { location.hash = `/${name}/${args.join('/')}`; }; window.Route = Route; -Window.routeTo = routeTo; \ No newline at end of file +window.routeTo = routeTo; \ No newline at end of file diff --git a/package.json b/package.json index 38f0b55..ade81e7 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "vanjs-core": "^1.2.7" }, "name": "vanjs-router", - "version": "1.2.1", + "version": "1.2.2", "description": "基于 Van.js 的前端路由控制系统", "main": "js/index.js", "types": "src/index.ts", diff --git a/src/index.ts b/src/index.ts index 395357c..0ad7923 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import van, { ChildDom, PropValueOrDerived } from 'vanjs-core' +import van, { ChildDom, PropValueOrDerived, Props, PropsWithKnownKeys } from 'vanjs-core' const { div } = van.tags @@ -33,19 +33,19 @@ window.addEventListener('hashchange', () => { activeRoute.val = nowRoute() }) -const Route = (config: RouteSetting, ...rest: readonly ChildDom[]) => { - const { name, onFirst, onLoad, ...otherProp } = config +const Route = (first: RouteSetting & Props & PropsWithKnownKeys, ...rest: readonly ChildDom[]) => { + const { name, onFirst, onLoad, ...otherProp } = first let firstLoad = true van.derive(() => { - if (activeRoute.val.name == config.name) { - if (firstLoad && config.onFirst) { - config.onFirst(activeRoute.val) + if (activeRoute.val.name == first.name) { + if (firstLoad && first.onFirst) { + first.onFirst(activeRoute.val) firstLoad = false } - if (config.onLoad) config.onLoad(activeRoute.val) + if (first.onLoad) first.onLoad(activeRoute.val) } }) - return div({ hidden: () => config.name != activeRoute.val.name, ...otherProp }, rest) + return div({ hidden: () => first.name != activeRoute.val.name, ...otherProp }, rest) } const routeTo = (name: Route['name'] = 'home', args: any[] = []) => {