A Router Module for Single Page Application
- support
beforeEnter
,beforeLeave
,beforeEachEnter
,beforeEachLeave
-
createLink
method -
setUrl
changed tosetUrlOnly
- 新增
name
选项,name
可以作为go
,dispatch
,setUrlOnly
等方法的参数 -
go
,dispatch
,setUrlOnly
等方法支持传入路由描述对象作为参数 - 新增
data
选项,可以通过 Req 对象传给回调函数使用 - 支持异步回调阻塞(即必须等待异步回调完成才调用下一个回调)
- 支持
go
,back
等历史操作 - 增加
destroy
方法销毁路由器 - 移除
configure
函数,配置应该在创建路由实例的时候完成 - 支持 recurse 模式,即在寻找路由匹配的过程中,如果路由完全匹配完整路径的前缀部分,也可以触发 dispatch
- 公开 Router.QS 对象,提供操作 query string 的两个重要方法
- 移除
setRoute
方法,用go
代替 - 移除
redirect
方法,用go
代替 - 移除
root
配置项 - 支持
*
匹配 - 移除
notFound
配置项,使用*
匹配
spa-router-better
is a router module for building large single-page-application(SPA).If you are using vue.js, it's easy for you to handle routing.
npm install && npm start
npm install spa-router-better
OR
Use the dist files in the dist folder.
- config your routes
- create a new
Router
- invoke
.start()
method
const routes = {
"/": {
"name": "home",
"controllers": [homeController],
"sub": {
"/product": {
"name": "productList",
"beforeLeave": [doBeforeLeave],
"controllers": [productController1, productController2],
"beforeEnter": [doAfterEnter],
"data": {
"custom": "data"
}
},
"*": {
"name": "pageNotFound",
"controllers": []
}
}
}
};
Router.mode('hashbang');
const configs = {
mode: "hashbang", // default: hashbang
beforeEachEnter: [],
beforeEachLeave: []
};
const myRouter = new Router(routes, configs);
myRouter.start();
named-route
callbacks
callbacks which will be called before controllers
callbacks which will be called before switch to another route
custom route data
sub routes
You can get params or query from the req
argument in the callback handler.
How to define a param:
:paramName
, theparamName
matches[a-zA-Z0-9_]+
:paramName(matchRule)
, thematchRule
is a RegExp string- if you insert a
matchRule
without aparamName
, the match rule still works, but you cannot get the params fromreq.params
How to get the params:
req.params
Examples:
var routes = {
'/product/:color(red|blue|black)-:size-:price([0-9]{1,2})': function(req) {
var params = req.param;
console.log('product list with ' + params.color + ' color, ' + params.size + ' size and ' + params.price + ' price');
}
};
Example
var routes = {
'/product': function(req) {
var query = req.query;
// current request url is "/produce?color=red&size=normal&price=low"
console.log(query.color, query.size, query.price);
// log: red normal low
}
}
Notice:
a=1&a=2
will be parsed into{"a":['1','2']}
- if the query is not a
key:value
mapping, it will not be parsed, e.g.a&b=2
will be parsed into{"b":"2"}
+
in query string will be parsed into a whitespace string" "
, e.g.a=1+1
will be parsed into{"a":"1 1"}
. If you need to pass a real+
string from the query, you should wrap the query string withencodeURIComponent
, e.g.encodeURIComponent('a=1+1')
will be parsed into{"a":"1+1"}
- all the query strings will be parsed into an
Object
orArray
new Router(options);
options:
mode
(String)beforeEachEnter
(Function|Array)beforeEachLeave
(Function|Array)
start the router
Options
root
:[String]
where to beginnotFound
[Function]
this function will be called if current url do not match any routesmode
['history'|'hashbang']
url mode,history
need HTML5 History API support
add a route to the route table
router.on('/test', function(req) {
// ...
});
- this method will merge the route with current route table
- if this method is called after the
.start()
method, this route will not dispatch immediately, you must wait until next time the url change or you can calldispatch
method to trigger the route
remove a route from route table
merge another route table with current route table
- routes will be merged with current route table
- if this method is called after the
.start()
method, this route will not dispatch immediately, you must wait until next time the url change or you can calldispatch
method to trigger the route
dispatch a route
change the url to path
and dispatch a route
- if the path has not changed, no routes will be dispatched, so do not use this method to reload a page
reload the page(dispatch current route again)
- no arguments