npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-guid

v1.1.8

Published

react-router v6 路由统一管理及路由拦截方案

Downloads

2

Readme

react-router-guid

react-router v6 路由统一管理及路由拦截方案。

  • 版本要求:react-router-dom >= 6.2.0
  • 支持TS

1、安装

npm i react-router-guid -S

2、使用

// 在项目入口文件index.js或入口组件App.js里引入
import { HashRouter } from 'react-router-dom' // 引入官方路由组件
import RouterWaiter from 'react-router-guid' // 引入该插件
import routes from './routes' // 引入你的路由配置
import onRouteBefore from './onRouteBefore' // 引入你定义的路由拦截函数

function App () {
  return (
    <HashRouter>
      <RouterWaiter routes={routes} onRouteBefore={onRouteBefore} />
    </HashRouter>
  )
}

export default App

3、配置路由

const Index = () => import(/* webpackChunkName: "index" */ '@/views/index/index')

const routes = [
  {
    path: '/',
    redirect: '/index', // redirect,要重定向的路由路径
  },
  {
    path: '/index',
    component: Index, // component,懒加载方式引入的组件
    meta: { // meta,自定义的数据
      title: '首页',
      needLogin: true,
    },
  },
]

export default routes
  • 目前支持配置的字段有 redirect、component、meta,其他字段和react-router-dom的官方支持字段保持一致。(优先级:redirect > component > element。)
  • 嵌套路由的使用请看下面的注意事项。

4、配置路由拦截函数

/**
 * @param {string} pathname 当前路由路径
 * @param {object} meta 当前路由自定义meta字段
 * @return {string} 需要跳转到其他页时,就返回一个该页的path路径,或返回resolve该路径的promise对象
 */
const onRouteBefore = ({ pathname, meta }) => {
  // 动态修改页面title
  if (meta.title !== undefined) {
    document.title = meta.title
  }
  // 判断未登录跳转登录页
  if (meta.needLogin) {
    if (!isLogin) {
      return '/login'
    }
  }
}

export default onRouteBefore

5、API

组件 RouterWaiter 的配置属性 API:

  • routes,数组类型,路由配置数组(必填)
  • onRouteBefore,函数类型,路由拦截函数(可选)
  • loading,组件类型,懒加载路由切换时的 loading 效果组件,默认为一个空div标签(可选)

6、注意事项

  • react-router 的嵌套路由父级不支持懒加载方式引用公共组件,需改用官方的element方式。
import PageLayout from '@/components/PageLayout' // 静态引入,不要使用import函数

{
  path: '/',
  element: <PageLayout />, // 父级的公共组件需使用element配置
  children: [
    ... // 子级可以继续使用component懒加载方式
  ]
},

7、TS类型

export type {
  MetaType, // 路由meta字段类型
  FunctionalImportType, // 懒加载函数式导入组件的类型
  ReactCompType, // react组件类型
  RoutesItemType, // 路由配属数组项类型
  RoutesType, // 路由配置数组类型
  OnRouteBeforeResType, // 路由拦截函数(实际有效使用的)返回值类型
  OnRouteBeforeType, // 路由拦截函数类型
  RouterWaiterPropsType, // RouterWaiter主组件props类型
  RouterWaiterType, // RouterWaiter主组件类型
}