Code Monkey home page Code Monkey logo

Comments (1)

songyanbo avatar songyanbo commented on June 8, 2024

可以参考ant-design-pro的权限,只需要在utils/core.js做如下更改即可实现,还有记得在具体的页面路由配置中增加authority: ['admin'],代码如下:
`import React from 'react';
import dynamic from 'dva/dynamic';
import { Route, Switch, Redirect } from 'dva/router';
import DocumentTitle from 'react-document-title';
import assign from 'object-assign';
import $$ from 'cmn-utils';
import config from '@/config';
import Authorized from '../utils/Authorized';
const { AuthorizedRoute } = Authorized;

/**

  • 生成动态组件
  • @param {*} app
  • @param {*} models
  • @param {*} component
    */
    export const dynamicWrapper = (app, models, component) =>
    dynamic({
    app,
    models: () => models,
    component
    });

/**

  • 生成一组路由
  • @param {*} app
  • @param {*} routesConfig
    /
    export const createRoutes = (app, routesConfig) => {
    return (

    {routesConfig(app).map(config => createRoute(app, () => config))}

    );
    };
    // 路由映射表
    window.dva_router_pathMap = {};
    /
    *
  • 生成单个路由
  • @param {*} app
  • @param {*} routesConfig
    /
    export const createRoute = (app, routesConfig) => {
    const {
    component: Comp,
    path,
    indexRoute,
    title,
    exact,
    authority,
    ...otherProps
    } = routesConfig(app);
    if (path && path !== '/') {
    window.dva_router_pathMap[path] = { path, title, ...otherProps };
    // 为子路由增加parentPath
    if (otherProps.childRoutes && otherProps.childRoutes.length) {
    otherProps.childRoutes.forEach(item => {
    if (window.dva_router_pathMap[item.key]) {
    window.dva_router_pathMap[item.key].parentPath = path;
    }
    });
    }
    }
    const routeProps = assign(
    {
    key: path || $$.randomStr(4),
    render: props => (
    <DocumentTitle
    title={
    config.htmlTitle ? config.htmlTitle.replace(/{.
    }/gi, title) : title
    }
    >
    <Comp routerData={otherProps} {...props} />

    )
    },
    path && {
    path: path
    },
    exact && {
    exact: exact
    },
    authority&&{
    authority: authority
    }
    );
    // before add authorized
    // if (indexRoute) {
    // return [
    // <Redirect key={path + '_redirect'} exact from={path} to={indexRoute} />,
    // <Route {...routeProps} />
    // ];
    // }

// return <Route {...routeProps} />;

if (indexRoute) {
return [
<Redirect key={path + '_redirect'} exact from={path} to={indexRoute} />,
<AuthorizedRoute
{...routeProps}
redirectPath="/403"
/>
];
}
return <AuthorizedRoute
{...routeProps}
redirectPath="/403"
/>;
};
页面路由配置更改:import { dynamicWrapper, createRoute } from '@/utils/core';

const routesConfig = app => ({
path: '/myproject',
title: '我的项目',
component: dynamicWrapper(app, [import('./model')], () => import('./components')),
exact: true,
authority: ['admin','audit'],
});

export default app => createRoute(app, routesConfig);
`

from dva-boot-admin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.