Skip to content

Commit

Permalink
version 1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
iuroc committed Jan 9, 2024
1 parent 8d5c81a commit 9616932
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install vanjs-core vanjs-router

```html
<script src="https://cdn.jsdelivr.net/gh/vanjs-org/van/public/van-1.2.7.nomodule.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/iuroc/vanjs-router/js/vanjs-router.1.2.1.js"></script>
<script src="https://cdn.jsdelivr.net/gh/iuroc/vanjs-router/js/vanjs-router.1.2.2.js"></script>
```

### Import and Usage
Expand Down
16 changes: 8 additions & 8 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 9 additions & 8 deletions js/vanjs-router-1.2.1.js → js/vanjs-router-1.2.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -36,4 +37,4 @@ const routeTo = (name = 'home', args = []) => {
location.hash = `/${name}/${args.join('/')}`;
};
window.Route = Route;
Window.routeTo = routeTo;
window.routeTo = routeTo;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import van, { ChildDom, PropValueOrDerived } from 'vanjs-core'
import van, { ChildDom, PropValueOrDerived, Props, PropsWithKnownKeys } from 'vanjs-core'

const { div } = van.tags

Expand Down Expand Up @@ -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<HTMLDivElement>, ...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[] = []) => {
Expand Down

0 comments on commit 9616932

Please sign in to comment.