react-view-router@2.x
A Route manager for
react
can write routing configuration like vue-router in react. see: Nested Routes
Unlike 1.x,
react-view-router@2.x
does not depend on the react-router Library
Installation
npm install react-view-router --save# or yarn add react-view-router
Basic Usage
/// router.js; const router = base: '' // the base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/" mode: 'hash' // or browser|memory|hash, default:hash routes: // also can be passed by router.use method; ;
/// app.js;;;; router; router; { const filter = routes; return <div> <h1>App</h1> <RouterView router=router filter=filter fallback=<div>loading</div> /> </div> ;}
/// home/index.js;; { return <div> <h1>HomeIndex</h1> <RouterView /> </div> ;}
/// home/main/index.js;; { return <div> <h1>HomeMainIndex</h1> <RouterView /> <RouterView name="footer" /> </div> ;}
/// home/home/main/some/index.js;;; Component { superprops; thisstate = text: 'text1' ; } { this } { let text = thisstate; return <div> <h1>HomeMainSomeIndex</h1> text </div> ; } HomeMainSomeIndex { if !storelogined ; else ; } { // confirm leave prompt ; } { } { };
/// home/main/some/footer.js; { return <div> <h1>HomeMainSomeFooter</h1> </div> }
/// login/index.js;;; { const doLogin = { storelogined = true; router; }; return <div> <h1>LoginIndex</h1> <button onClick=doLogin>Login</button> </div> ;}
/// routes.js;;; const routes =
APIs
Route Option
path
URL string.component
React component.components
React components that be used forNamed RouterView
.exact
Whether only matches withlocation.pathname
exactly.strict
When true, a path that has a trailing slash will only match a location.pathname with a trailing slash. This has no effect when there are additional URL segments in the location.pathname.redirect
Navigates to new location, can be string, object or function.index
index route name, can be string or function.children
Nested child routes.meta
some custom route infos, see: Route Meta Fields.defaultProps
object{ aa: 1, bb: '2', cc: true }
, give some props into route component.props
boolean or object{ aa: Number, bb: String, cc: Boolean }
, Pass url params as a prop into route component.paramsProps
boolean or object{ aa: Number, bb: String, cc: Boolean }
, Pass url params as props into route component.queryProps
boolean or object{ aa: Number, bb: String, cc: Boolean }
, Pass url query as props into route component.guards
the route guards, see:Per-Route Guard
RouterView Props
name
Use forNamed Views
, see vue-router instructionsfilter
is a function:function (routes: Array) { return [] }
that use for filter routesfallback
can be a functionfunction ({ parentRoute, currentRoute, inited, resolving, depth }) { return <div /> }
, orReact Component Element
like this:<Loading>loading</Loading>
RouterView Instance Methods
RouterView.push(routes: Array): Array
add routes to RouterView instance, likeArray.push
;RouterView.splice(start[, deleteCount[, item1[, item2[, ...]]]]): Array
delete routes in RouterView instance, likeArray.splice
;RouterView.indexOf(route: String|Object): Number
checkroute
is in RouterView, likeArray.indexOf
RouterView.remove(route: String|Object): Object
removeroute
from RouterView, returnremoved route
orundefined
ReactViewRouter Props
currentRoute
current matched route infos:
url: String // A string that equals the path of the current route, always resolved as an absolute path path: String // The full resolved URL including query and hash. fullPath: String // true if the path matches the location.pathname exactly. isExart: Boolean // An Array containing route records for all nested path segments of the current route. matched: route1 // route2: // the path from route config path: String // the subpath from route config subpath: String // the meta from route config meta: Object // the redirect from route config redirect: String|Object|Function // the original route config config // the component instance that matched this route config if found. componentInstances: default: ReactComponent /* others: React.Component */ // the RouterView instance that matched this route config if found. viewInstances: default: RouterView ... routeN // An object that contains key/value pairs of dynamic segments and star segments. If there are no params the value will be an empty object. params: Object // An object that contains key/value pairs of the query string. For example, for a path /foo?user=1, we get currentRoute.query.user == 1. If there is no query the value will be an empty object. query: Object // The name of the route being redirected from, if there were one redirectedFrom: Object // The route abort function that passed by `router.push`, `router.replace`, `router.redirect` onAbort: Function // The route complete function that passed by `router.push`, `router.replace`, `router.redirect` onComplete: Function
ReactViewRouter instance Props
RouterLink
aNavLink
component likeroute-link
invue-router
.currentRoute
the current route that matched current url.initialRoute
the initial route when router be created.
ReactViewRouter instance Methods
beforeEach
global before guardsbeforeResolve
global brefore resolve guardsafterEach
global after guardspush
、replace
、go
、back
、forward
redirect
history navigation methodsparseQuery
、stringifyQuery
Provide custom query string parse / stringify functions, can be override bynew ReactViewRouter({ parseQuery: parseQueryMethod, stringifyQuery: stringifyQueryMethod });
install
ReactVueLike
plugin install methods. see: ReactVueLike
Export Methods
useRouteGuards
route component guards methods:
/** * route component guards methods * @param * @param * @param * @return **/
lazyImport
route component lazy load method:
/** * route component lazy load method * @param * @return **/ {}
normalizeRoutes
normalize route configs:
/** * normalize route configs * @param * @param * @return **/
normalizeLocation
normalize location string or object:
/** * normalize location string or object * @param * @param * @return **/
isLocation
determine whetherv
is a location object
/** * determine whether `v` is a location object * @param * @return **/ {}
matchPath
just re-export, see: matchPath
NOTE
- You sholud config the
Webpack Configuration
withalias
:
... resolve: ... alias: history: 'history-fix' ...
otherwise, webpack will package both history
and history-fix
into the target js file.
- if route component is
Class Component
(notFunction Component
), thenthis
variable inbeforeRouteUpdate
,beforeRouteLeave
,afterRouteLeave
Component Guards will be that component instance;