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

@huxy/vue-router

v0.0.6

Published

vue use router

Downloads

8

Readme

useRouter

根据popstate、hashchange监听路由变化,

const historyEvent=browserRouter?'popstate':'hashchange';
const changeEvent=()=>changeRouter();
window.addEventListener(historyEvent,changeEvent,false);

路由切换。history.pushState history.replaceState

const historyEvents=(path,params,event='pushState')=>{
  history[event](params,null,path);
  changeRouter(path,params);
};
const push=({path,params})=>{
  historyEvents(path,params);
};
const replace=({path,params})=>{
  historyEvents(path,params,'replaceState');
};

根据当前路由找到对应components

const formatMenu=(router,path,params)=>{
  const {result,current}=getCurrent(router,path,params);
  const menu=filterMenu(result);
  return {
    current,
    menu,
  };
};

lazyload components

const loader=async props=>{
  let component=await props.component;
  component=component.default??res;
  return component;
};

fetch data

const fetchData=async ()=>{
  try{
    const result=await resolve(params);
    setPropsResolve(result);
  }catch(err){
    setPropsResolve(err);
  }
};

errorBoundary

export default class ErrorBoundary extends React.Component{
  state={
    error:null,
  };
  static getDerivedStateFromError(error){
    return {error};
  }
  render(){
    const {error}=this.state;
    const {fallback,children}=this.props;
    if(error){
      return fallback(error);
    }
    return children;
  }
}

<ErrorBoundary>
  <Suspense fallback={handleLoading(Loading)}>
    {comp}
  </Suspense>
</ErrorBoundary>

Link

const Link=props=>{
  const {to,children,...rest}=props;
  const handleClick=e=>{
    e.preventDefault();
    emit(pushEvent,to);
  };
  return <a onClick={e=>handleClick(e)} href={to?.path??to??''} {...rest}>{children}</a>;
};

配置示例

install

npm i @huxy/router

使用

const App=()=>{
  const {components,loading}=useRouter(yourConfigs);
  return <>
    {components}
    {loading&&<Spinner global />}
  </>;
};

配置

全局配置

const browserRouter=!process.env.isDev;
const idKey='url';
const title='项目管理平台';
const theme='dark';
let whiteList=['/dashboard/app4'];
whiteList=browserRouter?whiteList:whiteList.map(v=>`#${v}`);
const beforeRender=input=>{
  if(whiteList.includes(input.path)){
    return {path:'/sign/signin'};
  }
};
const afterRender=output=>{
  console.log(output);
};

export default {
  browserRouter,
  idKey,
  title,
  theme,
  beforeRender,
  // afterRender,
};
  • browserRouter:是否为browserHistory
  • idKey:路由key值。如:url
  • childKey:子层级key值。如:children
  • title:页面title。
  • beforeRender:路由守卫,渲染前回调。
  • afterRender:渲染完回调。
  • basepath:路由前缀。
  • others:其它设置。

组件配置

url:'/dashboard',
redirect:'/dashboard/app1',
name:'Dashboard',
icon:'icon-dashboard',
children:[
  {
    url:'/app1',
    name:'app1',
    icon:'icon-th-list',
    component:<App1 />,
    loadData:{
      user:fetchUser,
    },
    errorBoundary:true,
  },
  {
    url:'/app2',
    name:'app2',
    icon:'icon-th-list',
    component:()=>import('./app2'),
    denied:true,
  },
  {
    url:'/app3',
    name:'app3',
    icon:'icon-th-list',
    component:()=>import('./app3'),
    resolve:{
      user:fetchUser,
    },
  },
  {
    url:'/app4',
    name:'admin',
    icon:'icon-th-list',
    component:<h1>app4</h1>,
    hideMenu:false,
  },
]
  • path:路径
  • name:展示名
  • icon:图标
  • redirect:重定向
  • children:子菜单
  • component:页面
  • denied:权限控制
  • hideMenu:菜单隐藏展示
  • errorBoundary:错误边界。默认有错误边界处理,可自定义。
  • resolve:数据请求并缓存,可用store.getState(key)获取,store.setState(state)更新。
  • loadData:数据请求,不缓存数据。

工具函数

  • store:提供数据全局管理功能。setState、getState、subscribe
  • eventBus:订阅发布功能。on、emit、off
  • updateRouter:更新路由配置

示例

const childRouters=[dashboard,...user,pages];

const routers=[
  {
    url:'/',
    redirect:initPath,
    component:()=>import('../layout/frame'),
    resolve:{
      user:fetchUserInfo,
    },
    getMenus:true,
    frameTheme:'dark',
    children:childRouters,
  },
  {
    url:'/sign',
    name:'登录',
    title:'登录',
    component:<div>user</div>,
    hideMenu:true,
    children:[
      {
        url:'/signin',
        name:'登录',
        component:<h1>登录</h1>,
      },
      {
        url:'/signup',
        name:'注册',
        component:()=><h1>注册</h1>,
      },
    ],
  },
  {
    url:'/404',
    name:'404',
    component:import('../views/404'),
    hideMenu:true,
  },
];

const dashboard={
  url:'/dashboard',
  redirect:'/dashboard/app1',
  name:'Dashboard',
  icon:'icon-dashboard',
  children:[
    {
      url:'/app1',
      name:'app1',
      icon:'icon-th-list',
      component:()=>import('./app1'),
    },
    {
      url:'/app2',
      name:'app2',
      icon:'icon-th-list',
      component:()=>import('./app2'),
    },
    {
      url:'/app3',
      name:'app3',
      icon:'icon-th-list',
      component:()=>import('./app3'),
      resolve:{
        user:fetchUser,
      },
    },
  ],
};

const user=[
  {
    url:'/user',
    name:'用户管理',
    icon:'icon-id-card',
    component:()=>import('./user'),
    resolve:{
      users:fetchUsers,
    },
  },
  {
    url:'/user/:id',
    name:'src',
    icon:'icon-th-list',
    component:()=>import('./user/src'),
    resolve:{
      src:fetchUserSrc,
    },
  },
  {
    url:'/user/:id/:type',
    name:'srcList',
    icon:'icon-th-list',
    component:()=>import('./user/srcList'),
    resolve:{
      srcList:fetchUserSrcList,
    },
  },
];

const pages={
  url:'/pages',
  name:'页面管理',
  title:'页面管理',
  icon:'icon-desktop',
  redirect:'/pages/400',
  children:[
    {
      url:'/400',
      name:'400',
      icon:'icon-th-list',
      redirect:'/pages/400/401',
      children,
    },
    {
      url:'/500',
      name:'500',
      icon:'icon-th-list',
      component:()=>import('./500'),
    },
  ],
};