From 8b743adb2b70d1521a5120985ab99f9025e3409d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Mon, 22 Oct 2018 20:38:02 +0800 Subject: [PATCH] menu decoupling umi/local --- src/components/SiderMenu/BaseMenu.js | 5 ++-- src/layouts/BasicLayout.js | 38 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/components/SiderMenu/BaseMenu.js b/src/components/SiderMenu/BaseMenu.js index 0fc635de..427469bd 100644 --- a/src/components/SiderMenu/BaseMenu.js +++ b/src/components/SiderMenu/BaseMenu.js @@ -3,7 +3,6 @@ import { Menu, Icon } from 'antd'; import Link from 'umi/link'; import isEqual from 'lodash/isEqual'; import memoizeOne from 'memoize-one'; -import { formatMessage } from 'umi/locale'; import pathToRegexp from 'path-to-regexp'; import { urlToList } from '../_utils/pathTools'; import styles from './index.less'; @@ -83,7 +82,7 @@ export default class BaseMenu extends PureComponent { getSubMenuOrItem = item => { // doc: add hideChildrenInMenu if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) { - const name = item.locale ? formatMessage({ id: item.locale }) : item.name; + const { name } = item; return ( { - const name = item.locale ? formatMessage({ id: item.locale }) : item.name; + const { name } = item; const itemPath = this.conversionPath(item.path); const icon = getIcon(item.icon); const { target } = item; diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index aa29acc7..81b2ce88 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -24,30 +24,30 @@ const { Content } = Layout; function formatter(data, parentAuthority, parentName) { return data .map(item => { + if (!item.name || !item.path) { + return null; + } + let locale = 'menu'; - if (parentName && item.name) { + if (parentName) { locale = `${parentName}.${item.name}`; - } else if (item.name) { + } else { locale = `menu.${item.name}`; - } else if (parentName) { - locale = parentName; - } - if (item.path) { - const result = { - ...item, - locale, - authority: item.authority || parentAuthority, - }; - if (item.routes) { - const children = formatter(item.routes, item.authority, locale); - // Reduce memory usage - result.children = children; - } - delete result.routes; - return result; } - return null; + const result = { + ...item, + name: formatMessage({ id: locale, defaultMessage: item.name }), + locale, + authority: item.authority || parentAuthority, + }; + if (item.routes) { + const children = formatter(item.routes, item.authority, locale); + // Reduce memory usage + result.children = children; + } + delete result.routes; + return result; }) .filter(item => item); } -- GitLab