Skip to content

Commit

Permalink
update to 1.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
gxlmyacc committed Aug 20, 2019
1 parent 351c05f commit a89ce0d
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 57 deletions.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,63 @@ see: [Route Object Properties](https://router.vuejs.org/api/#route-object-proper
- `push``replace``go``back``forward` `redirect`[history navigation methods](https://router.vuejs.org/guide/essentials/navigation.html)
- `parseQuery``stringifyQuery` Provide custom query string parse / stringify functions, can be override by `new ReactViewRouter({ parseQuery: parseQueryMethod, stringifyQuery: stringifyQueryMethod });`

### Export Methods
- `useRouteGuards` route component guards methods:
```javascript
/**
* route component guards methods
* @param {Object} component - route component
* @param {Object} guards - guards methods
* @param {Class} [componentClass] - the route component class, it will be useful when component is High-order components
* @return {RouteComponentGuards} - the route componet that can be regarded as `React.forwardRef`
* /
function useRouteGuards(component, guards = {}, componentClass) {}
```
- `lazyImport` route component lazy load method:
```javascript
/**
* route component lazy load method
* @param {Function} importMethod - webpack lazy import method, For example: () => import('@/components/some-component')
* @return {RouteLazy} - the result only be used with component or components props in route config
* /
function lazyImport(importMethod) {}
```
- `normalizeRoutes` normalize route configs:
```javascript
/**
* normalize route configs
* @param {Array} routes - unnormalized route configs
* @param {Object} [parent] - if provide, routes will be resolved regarded as parert`s children
* @return {Array} - normalized route configs
* /
function normalizeRoutes(routes, parent) {}
```
- `normalizeLocation` normalize location string or object:
```javascript
/**
* normalize location string or object
* @param {Object|string} to - location that need to normalize
* @param {Object} [parent] - if provide, location will be resolved with parert
* @return {Object} - normalized location object: { path: string, pathname: string, search: string, query: Object, ...custom props }
* /
function normalizeLocation(to, parent) {}
```
- `isLocation` determine whether `v` is a location object
```javascript
/**
* determine whether `v` is a location object
* @param {Object} v - the object to be determined
* @return {boolean}
* /
function isLocation(v) {}
```
- `withRouter` just re-export, see: [withRouter](https://reacttraining.com/react-router/web/api/withRouter)
- `matchPath` just re-export, see: [matchPath](https://reacttraining.com/react-router/web/api/matchPath)
## NOTE
1. You sholud config the `Webpack Configuration` with `alias`:
```javascript
Expand All @@ -279,7 +336,7 @@ see: [Route Object Properties](https://router.vuejs.org/api/#route-object-proper
```
otherwise, webpack will package both `history` and `history-fix` into the target js file.
2. when route component is `Class Component` (not `Function Component`), the `this` in `afterRouteEnter`, `beforeRouteUpdate`,`beforeRouteLeave`,`afterRouteLeave` Component Guards and `afterEnter`,`beforeUpdate`,`beforeLeave`,`afterLeave` in Route Guards will be the component instance;
2. if route component is `Class Component` (not `Function Component`), then `this` variable in `afterRouteEnter`, `beforeRouteUpdate`,`beforeRouteLeave`,`afterRouteLeave` Component Guards and `afterEnter`,`beforeUpdate`,`beforeLeave`,`afterLeave` in Route Guards will be that component instance;
## License
Expand Down
128 changes: 99 additions & 29 deletions dist/react-view-router.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/react-view-router.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-view-router.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-view-router",
"version": "1.0.8",
"version": "1.0.9",
"description": "react-view-router",
"keywords": [
"react-view-router",
Expand Down
21 changes: 14 additions & 7 deletions src/route-guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export const REACT_LAZY_TYPE = LazyMeth.$$typeof;

export class RouteCuards {
constructor(guards, isComponentGuards = false) {
this.isComponentGuards = isComponentGuards;
this.beforeEnter = [];
this.beforeUpdate = [];
this.afterEnter = [];
this.beforeLeave = [];
this.afterLeave = [];

Object.defineProperty(this, 'isComponentGuards', { writable: true, configurable: true, value: isComponentGuards });

this.merge(guards || {}, isComponentGuards);
}

Expand All @@ -32,14 +34,19 @@ export class RouteCuards {
}
}

export function useRouteGuards(component, guards = {}) {
const ret = {
$$typeof: REACT_FORWARD_REF_TYPE,
render(props, ref) {
return React.createElement(component, { ...props, ref });
}
class RouteComponentGuards {
constructor() {
this.$$typeof = REACT_FORWARD_REF_TYPE;
}
}

export function useRouteGuards(component, guards = {}, componentClass) {
const ret = new RouteComponentGuards();
ret.render = function (props, ref) {
return React.createElement(component, { ...props, ref });
};
Object.defineProperty(ret, '__guards', { value: new RouteCuards(guards, true) });
Object.defineProperty(ret, '__component', { value: component });
Object.defineProperty(ret, '__componentClass', { value: componentClass });
return ret;
}
1 change: 1 addition & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export default class ReactViewRouter {
...last.match,
query: to.search ? qs.parseQuery(to.search.substr(1)) : {},
path: to.pathname,
hash: to.search,
fullPath: `${to.path}${to.search}`,
matched: matched.map(({ route }, i) => {
let ret = {};
Expand Down
Loading

0 comments on commit a89ce0d

Please sign in to comment.