我们要的是一个简单的react-router路由

我们要的是一个简单的react-router路由

我们要的是一个简单的react-router路由

习惯了 vue-router 路由的用法,再用react-router总感觉挺麻烦的。

那么react有没有用法跟vue-router一样使用简单的路由插件呢?

管它有没有,轮子我已经造好了,请收下。

react-concise-router

react-concise-router 是一个基于 react-router v4.x 封装的一个路由插件。

1、安装

直接安装

npm install -S react-concise-router

你还需要安装

npm install -S react-router
npm install -S react-router-dom

2、定义路由列表对象

router.js

import Router from 'react-concise-router'
import Home from './views/Home'
import User from './views/User'
import UserInfo from './views/UserInfo'
import ErrorPage from './views/Error'
import view from './views/admin/view'
import Dashboard from './views/admin/Dashboard'

const router = new Router ({
  mode: 'hash',
  routes: [
    {path: '/', component: Home},
    {path: '/user', component: User},
    {path: '/user/:userId', name: 'info', component: UserInfo},
    {
      path: '/admin',
      component: view,
      name: 'admin-view',

      children: [
        {path: '/', component: Dashboard},
        {path: '/test', component: Dashboard},
        {component: ErrorPage}
      ]
    },
    {path: '*', component: ErrorPage},
  ]
})

export default router

App.jsx

import React from 'react'
import router from './router'

export default class App extends React.Component {
  render () {
    return (
      <div>
        <p>wellcome !</p>
        <router.view />
      </div>
    )
  }
}

API

new Router(options) 创建路由对象,返回router。

  • options object 路由配置的对象
  • options.mode string 定义路由类型,hash|history
  • options.routes array 路由列表
  • options.routes[].name string 路由名称,如果当前存在children属性,表示路由出口名称
  • options.routes[].path string 路径
  • options.routes[].component Function 路由组件;如果当前存在children属性,表示子路由组件
  • options.routes[].children array 子路由列表
options.path 不存在或者为 * 路由会当做notMath路由,匹配404

router

  • router.route(route) 生成url,用于history.push。
  • router.beforeEach(cxt, next) 路由切换中间件
router.view

<router.view /> 是一个路由出口组件。

props

  • props.name string 路由出口子名称,默认'default';在 options.routes[].name 设置。
router.link

router.link 是一个类似于 Link 的组件。

props

  • props.to object|string 路径或者路径对象route。
router.beforeEach

router.beforeEach 是一个路由中间件,你可以做一些路由切换事件;比如授权拦截,重定向,等待等操作。

你应该把它定义为一个函数

router.beforeEach = function (ctx, next) {}
  • ctx 这个是一个上下文对象,{route, router, history,...}
  • next 这是一个回调函数,请在最后调用它;next 可以接受一个字符串路径或者对象,这样可以重定向到别的路由。

route

  • route.name string 命名路由name,优先于path
  • route.path string 路径
  • route.params object 路由参数对象
  • route.query object 查询字符串对象
  • route.hash string 链接hash

最后

安利一个详细的列子:ant-admin-platform

源码:react-concise-router

再说下:插件开发出来不久,可能存在一些问题,如果有问题,请提issues,感谢你的使用。
刚从事react开发不久,有什么不合理的可以在下方评论交流下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Router是React的一种路由管理工具,它允许我们在应用程序中建立路由,并通过不同的URL路径来加载不同的页面。 而react-router-config是React Router的一个附加库,它提供了一种以配置方式来定义应用程序路由的方法。 路由切入动画是指在切换页面时,为页面添加一些过渡效果和动画,以提升用户体验。 使用react-router-config实现路由切入动画的步骤如下: 1. 首先,在路由配置文件中定义各个页面的路由信息,并设置对应的组件。 2. 在路由配置文件中,为每个路由定义一个transition属性,用于标识该路由的过渡效果。 3. 在根组件中使用React Router提供的Switch组件来包裹所有路由,并使用TransitionGroup组件来包裹Switch组件。 4. 在根组件中使用自定义的AnimatedSwitch组件来替换React Router提供的Switch组件,并将路由配置文件传递给AnimatedSwitch组件。 5. 在AnimatedSwitch组件中根据当前路由的transition属性,为切换的页面添加不同的过渡效果和动画。 例如,可以定义一个FadeIn动画效果,在路由配置文件中为需要应用该动画效果的路由设置transition属性为'fade-in',然后在AnimatedSwitch组件中根据该属性为页面添加相应的CSS动画样式。 总而言之,使用react-router-config可以方便地配置应用程序的路由信息,并结合一些CSS动画库,可以实现各种炫酷的路由切入动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值