最近利用ant-design-pro开发项目要实现以前的项目嵌入到新项目里来,而且根据和后台的接口返回的数据显示侧边栏菜单。既然是是利用别人的架构那固然是从文档中找实现的方法,终于不负苦心人在https://pro.ant.design/docs/router-and-nav-cn文档那里找到初步的解决方法
进入src/layouts/Basilayout.js在官网中直接复制该代码,将原文件替换。
如今正式进入正题。api
个人是放在views的models下面因此代码以下
export default connect(({global, setting, views }) => ({
collapsed: global.collapsed,
layout: setting.layout,
…setting,
…views,
}))(BasicLayout);架构
componentDidMount() {
const { dispatch } = this.props;
dispatch({
type: ‘views/fetch’,
});
}
我调取的是下面views.js里面的effects的fetch方法
至于model里面怎么写能够看官方文档https://pro.ant.design/docs/server-cn
这里是我写的一个views的model
函数
import { getMenuList } from '@/services/api' import { getMenuSessionData, getMenuMapArrData } from '@/utils/utils.js' export default { namespace: 'views', state: { datas: [], urlValues: 'https://boss.icarbonx.com/lims-ui/baselab/qcPaths/exception', urlDatas: [], }, effects: { *fetch({ payload }, { call, put }) { const response = yield call(getMenuList, payload); console.log(response,'获得列表') yield put({ type: 'save', payload: response }) }, *changeurl({ payload }, { call, put }){ yield put({ type: 'change', payload: payload }) }, }, reducers: { save(state, action) { return { ...state, datas: getMenuMapArrData(action.payload), urlDatas: getMenuSessionData(action.payload), } }, change(state, action) { return { ...state, urlValues: action.payload, } }, } }
理想状况返回的数据是
[{
path: ‘/user’,
component: ‘…/layouts/UserLayout’,
routes: [
{ path: ‘/user’, redirect: ‘/user/login’ },
{ path: ‘/user/login’, component: ‘./User/Login’ },
{ path: ‘/user/register’, component: ‘./User/Register’ },
{ path: ‘/user/register-result’, component: ‘./User/RegisterResult’ },
],
}]post
现实状况返回的数据是:fetch
[ { "id": "dashboardws", "name": "Dashboard", "description": "Dashboard", "url": 'https://boss.xxx.com/qcScheme/qcPrograms', component: './View/home', "children": [] }, { "id": "knowledge", "name": "Knowledge Platform", component: './View/home', "url": null, "children": [ { "id": "gene", "name": "Gene", component: './View/home', "url": 'https://XXX.XXX.com/qcPaths/qualityProjectQuery', "children": null }, { "id": "phenotype", "name": "Phenotype", component: './View/home', "url": 'https://XXX.XXX.com/lims-ui/baselab/qcPaths', "children": null }, { "id": "microbes", "name": "Microorganism", component: './View/home', "url": 'https://boss.xxx.com/qcPaths/qcSamplesCheck', "children": null } ] }, { "id": "indicatorww", "name": "Index Platform", "url": 'https://baidu.com', "children": [] }, { "id": "report", "name": "Report Platform", "url": 'https://boss.xxx.com/limb/qcScheme/qcSamples', "children": [] } ]
这样的话不能直接用要先处理,个人话是经过utils.js里写一个名叫getMenuMapArrData的方法进行格式化将后台返回的数据处理成相似理想状态的格式,ui
const menuListData = [ { "id": "knowledge", "name": "Knowledge Platform", component: './View/home', "url": null, "routes": [ { "id": "gene", "name": "Gene", component: './View/home', "url": 'https://XXX.XXX.com/qcPaths/qualityProjectQuery', "children": null }, { "id": "phenotype", "name": "Phenotype", component: './View/home', "url": 'https://XXX.XXX.com/lims-ui/baselab/qcPaths', "children": null }, { "id": "microbes", "name": "Microorganism", component: './View/home', "url": 'https://boss.xxx.com/qcPaths/qcSamplesCheck', "children": null } ] }, ]
主要是你格式化的这里里面要有routes这个属性,系统才能识别出你的菜单,固然没有子级菜单能够不须要改。this
即在router.config.js
{
path: ‘/:post’,
component: ‘./View/home’,
},
{
path: ‘/:post/:id’,
component: ‘./View/home’,
},
通过个人实验这个要写在
{
component: ‘404’,
},
的前面才不会被404 重定向url
注意:若是原数据不是理想数据而又不处理,而是在这个getMenuData里处理,会发生一些意想不到的错误。spa
getMenuData() { const { datas, route: { routes }, } = this.props; const newRoutes = [...datas, ...routes] return memoizeOneFormatter(Array.from([...newRoutes])); }