前言
随着前端技术的不断发展,前端工程化越来越普及,npm 包作为前端工程化中的重要组成部分,为开发者提供了许多便利。本文就为大家介绍一个优秀的前端路由工具 sugar-router,使前端页面的管理更为便捷。
什么是 sugar-router?
sugar-router 是一个轻量级、灵活的前端路由工具,它可以根据路由信息和相关代码,动态地管理前端页面的展示和改变。sugar-router 支持在 web 端和移动端使用,支持 hash 模式和 history 模式。
sugar-router 的安装和使用
我们可以通过 npm 命令来安装 sugar-router:
npm install sugar-router -S
然后在项目中引入 sugar-router:
-- -------------------- ---- ------- ------ ----------- ---- --------------- ----- ------ - - - ----- ---- -- ---- ---------- ----- -- ---- ------ ----- -- --------- --------- --------- -- ------- ---------- ----- -- ------ -------- -- -- - -------------------- -- -- ---------------- -------- -- -- - -------------------- -- -- ---------------- ------- -- -- - -------------------- -- -- ---------------- -- -- --- -- ----- ------- - - ----- ------- -- --------- --------- - ------ ----- ---- -- ------ ------------ ---- -- ---------------- -- ----- ------ - --- ------------------- ---------展开代码
当然,我们也可以通过 script 标签引入 sugar-router:
-- -------------------- ---- ------- ------- --------------------------------------------- -------- ----- ------ - - -- --- -- ----- ------- - - -- --- - ----- ------ - --- ------------------- --------- ---------展开代码
sugar-router 的配置
routes 配置
sugar-router 的主要配置项有 routes、options 两个。routes 是一个数组,用于配置路由信息。
-- -------------------- ---- ------- ----- ------ - - - ----- ---- -- ---- ---------- ----- -- ---- ------ ----- -- --------- --------- --------- -- ------- ---------- ----- -- ------ -------- -- -- - -------------------- -- -- ---------------- -------- -- -- - -------------------- -- -- ---------------- ------- -- -- - -------------------- -- -- ---------------- -- -- --- --展开代码
- path:路由路径,如 '/'、'/about'、'/contact' 等。
- component:组件名称,当 url 路径与当前路由匹配时,需要渲染的组件。
- cache:是否需要缓存该组件,如果为 true,则缓存该组件,否则每次进入路由时都会重新渲染组件。
- redirect:是否需要重定向,如果为 true,则重定向到指定路由。
- needLogin:是否需要登录,如果为 true,则该路由需要登录状态,否则不需要。
- onEnter:在进入该路由页面前执行的钩子函数。
- onLeave:在离开该路由页面前执行的钩子函数。
- onBack:在返回该路由页面时执行的钩子函数。
options 配置
options 是 sugar-router 的一些基本配置项:
const options = { mode: 'hash', // 路由模式,可选值为 'history' 或 'hash' base: '/', // 应用的基路径 animateTime: 300, // 切换页面时的动画时间,单位为毫秒 };
- mode:路由模式,可选值为 'history' 或 'hash',默认值为 'hash'。
- base:应用的基路径,默认为 '/'。
- animateTime:切换页面时的动画时间,单位为毫秒,默认值为 300。
sugar-router 的 API
sugar-router 提供了一些 API,方便我们在代码中控制路由。
push
router.push(path, state);
- path:要跳转的路由路径。
- state:可选参数,当使用 history 模式时传递的路由状态。
使用 push 方法跳转到新的路由页面。
replace
router.replace(path, state);
- path:要跳转的路由路径。
- state:可选参数,当使用 history 模式时传递的路由状态。
使用 replace 方法替换当前路由页面。
go
router.go(step);
- step:要退回或前进的步数,可以为正数或负数。
使用 go 方法在路由历史中前进或后退指定的步数。
back
router.back();
使用 back 方法后退到之前浏览过的路由页面。
forward
router.forward();
使用 forward 方法前进到之前浏览过的下一个路由页面。
getCurrentPath
router.getCurrentPath();
获取当前路由页面所在的路由路径。
getCurrentState
router.getCurrentState();
获取当前路由状态。
addRoutes
router.addRoutes(routes);
- routes:要新增的路由配置项。
使用 addRoutes 方法动态地添加新的路由配置项。
sugar-router 的事件
sugar-router 提供了一些事件,方便我们在代码中对路由的状态进行监听。
on('beforeInit')
router.on('beforeInit', () => { console.log('route is initializing'); });
在路由初始化前触发。
on('afterInit')
router.on('afterInit', () => { console.log('route has been initialized'); });
在路由初始化后触发。
on('beforeChange')
router.on('beforeChange', () => { console.log('route is changing'); });
在路由改变前触发。
on('afterChange')
router.on('afterChange', () => { console.log('route has been changed'); });
在路由改变后触发。
on('notFound')
router.on('notFound', () => { console.log('route not found'); });
在路由未找到时触发。
sugar-router 的示例代码
-- -------------------- ---- ------- ------ ----------- ---- --------------- ------ ---- ---- ------------------------ ------ ----- ---- ------------------------- ------ ------- ---- --------------------------- ----- ------ - - - ----- ---- ---------- ----- ------ ----- -- - ----- --------- ---------- ------ ------ ----- -- - ----- ----------- ---------- -------- ------ ----- -- -- ----- ------- - - ----- ------- ----- ---- ------------ ---- -- ----- ------ - --- ------------------- --------- ------------------------- -- -- - ------------------ -- ----------- --- ------------------------ -- -- - ------------------ --- ---- ---------- --- --------------------- -- -- - ------------------ --- -------- ---展开代码
结语
sugar-router 提供了一种简单、轻量级的前端路由方法,使得我们可以更加方便地管理前端页面。本文简要介绍了 sugar-router 的安装和使用、配置和 API、事件和示例代码等相关内容,希望可以对大家的学习和工作有所帮助。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60055aa581e8991b448d81f5