‘forEach’ undefined[pkg.json]

  1. TypeError: Cannot read property 'forEach' of undefined
  1. function AuthComponent({ children, location, routerData }) {
  2. const auth = getAuthority();
  3. const isLogin = auth && auth[0] !== 'guest';
  4. const getRouteAuthority = (path, routeData) => {
  5. let authorities;
  6. routeData.forEach(route => {
  7. // match prefix
  8. if (pathToRegexp(`${route.path}(.*)`).test(path)) {
  9. authorities = route.authority || authorities;
  10. // get children authority recursively
  11. if (route.routes) {
  12. authorities = getRouteAuthority(path, route.routes) || authorities;
  13. }
  14. }
  15. });
  16. return authorities;
  17. };
  18. return (
  19. <Authorized
  20. authority={getRouteAuthority(location.pathname, routerData)}
  21. noMatch={isLogin ? <Exception403 /> : <Redirect to="/user/login" />}
  22. >
  23. {children}
  24. </Authorized>
  25. );
  26. }
  27. export default connect(({ menu: menuModel }) => ({
  28. routerData: menuModel.routerData,
  29. }))(AuthComponent);

原因在 package.json

‘umi’ dependencies

  1. 'umi' should be listed in the project's dependencies

原因 dependencies 中没有添加 umi,误添加到 devDependencies 了

  1. "dependencies": {
  2. ......
  3. "umi": "^2.8.20",
  4. "umi-plugin-locale": "^2.9.13",
  5. "umi-plugin-react": "^1.9.19",
  6. "umi-request": "^1.0.0"
  7. },
  8. "devDependencies": {
  9. ......
  10. "umi-plugin-pro-block": "^1.2.0"
  11. },
  1. umi-plugin-react/locale should be listed in the project's dependencies

解决:终端安装对应依赖

  1. npm i -S umi-plugin-react
  1. npm i -S umi-plugin-locale

config

import not defined
两个 import 未定义,导致出了很多错,不知道是不是config 不允许不被赋值的情况

  1. import webpackPlugin from './plugin.config';
  2. import slash from 'slash2';

完整版

  1. // ref: https://umijs.org/config/
  2. import os from 'os';
  3. import pageRoutes from './router.config';
  4. import defaultSettings from '../src/defaultSettings';
  5. const { pwa, primaryColor } = defaultSettings;
  6. const { APP_TYPE, TEST } = process.env;
  7. const plugins = [
  8. [
  9. 'umi-plugin-react',
  10. {
  11. antd: true,
  12. dva: {
  13. hmr: true,
  14. },
  15. locale: {
  16. enable: true, // default false
  17. default: 'zh-CN', // default zh-CN
  18. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  19. },
  20. dynamicImport: {
  21. loadingComponent: './components/PageLoading/index',
  22. webpackChunkName: true,
  23. level: 3,
  24. },
  25. pwa: pwa
  26. ? {
  27. workboxPluginMode: 'InjectManifest',
  28. workboxOptions: {
  29. importWorkboxFrom: 'local',
  30. },
  31. }
  32. : false,
  33. ...(!TEST && os.platform() === 'darwin'
  34. ? {
  35. dll: {
  36. include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
  37. exclude: ['@babel/runtime', 'netlify-lambda'],
  38. },
  39. hardSource: false,
  40. }
  41. : {}),
  42. },
  43. ],
  44. ];
  45. // 针对 preview.pro.ant.design 的 GA 统计代码
  46. // 业务上不需要这个
  47. if (APP_TYPE === 'site') {
  48. plugins.push([
  49. 'umi-plugin-ga',
  50. {
  51. code: 'UA-72788897-6',
  52. },
  53. ]);
  54. }
  55. export default {
  56. plugins: [
  57. [
  58. 'umi-plugin-react',
  59. {
  60. antd: true,
  61. dva: {
  62. hmr: true,
  63. },
  64. targets: {
  65. ie: 11,
  66. },
  67. locale: {
  68. enable: true, // default false
  69. default: 'zh-CN', // default zh-CN
  70. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  71. },
  72. dynamicImport: {
  73. loadingComponent: './components/PageLoading/index',
  74. },
  75. },
  76. ],
  77. [
  78. 'umi-plugin-pro-block',
  79. {
  80. moveMock: false,
  81. moveService: false,
  82. modifyRequest: true,
  83. autoAddMenu: true,
  84. },
  85. ],
  86. ], /**
  87. * webpack 相关配置
  88. */
  89. define: {
  90. APP_TYPE: process.env.APP_TYPE || '',
  91. },
  92. treeShaking: true,
  93. targets: {
  94. ie: 11,
  95. },
  96. // Theme for antd
  97. // https://ant.design/docs/react/customize-theme-cn
  98. //路由相关配置
  99. routes: pageRoutes,
  100. disableRedirectHoist: true,
  101. theme: {
  102. 'primary-color': primaryColor,
  103. },
  104. externals: {
  105. '@antv/data-set': 'DataSet',
  106. },
  107. ignoreMomentLocale: true,
  108. lessLoaderOptions: {
  109. javascriptEnabled: true,
  110. },
  111. };

render

元素类型是无效的:将一个字符串(内置组件)或一个类/函数(用于复合组件)但有:定义。你可能忘了导出组件从文件中定义,或你可能会混违约和命名进口

  1. Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
  2. Check the render method of `Component`.
  3. (anonymous function)
  4. node_modules/react-dom/cjs/react-dom.development.js:24036
  5. 24033 | (function () {
  6. 24034 | {
  7. 24035 | {
  8. > 24036 | throw ReactError(Error('Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: ' + (type == null ? type : typeof type) + '.' + info));
  9. | ^ 24037 | }
  10. 24038 | }
  11. 24039 | })();

‘forEach’ of undefined

Cannot read property ‘forEach’ of undefined
  1. routeData.forEach(route => {
  2. // match prefix
  3. if (pathToRegexp(`${route.path}(.*)`).test(path)) {
  4. authorities = route.authority || authorities;
  5. // get children authority recursively
  6. if (route.routes) {
  7. authorities = getRouteAuthority(path, route.routes) || authorities;
  8. }
  9. }
  10. });

修改:使用前先判断是否存在

  1. if (!routeData){
  2. return;
  3. }
  4. routeData.forEach(route => {
  5. // match prefix
  6. if (pathToRegexp(`${route.path}(.*)`).test(path)) {
  7. authorities = route.authority || authorities;
  8. // get children authority recursively
  9. if (route.routes) {
  10. authorities = getRouteAuthority(path, route.routes) || authorities;
  11. }
  12. }
  13. });