如何使用dva.js在子路由上动态加载模型?

时间:2017-12-27 13:03:07

标签: reactjs antd

  1. 我想在子路径上动态加载模型,因为模型可能非常大,而且 模型有时是动态生成的(使用动态namespace)。 the target

  2. 这是我的代码。我从app

    导入了/src/index.js
    import React from 'react'
    import modelExtend from 'dva-model-extend'
    import { Route, Switch, Redirect } from 'react-router-dom'
    import dynamic from 'dva/dynamic'
    import app from '../../../index'
    
    function StepRoutes({ match }) {
      const id = match.params.id
      const routes = [{
        path: `${match.url}/class-maintenance`,
        models: () => [modelExtend(ClassBasicModel, { namespace: `class-maintenance-${id}` })],
        component: () => import('./ClassMaintenance'),
      }, {
        path: `${match.url}/course-selection`,
        models: () => [import('./CourseSelection/basicModel')],
        component: () => import('../Steps/CourseSelection'),
      }]
      return (
        <Switch>
          <Route exact path={match.url} render={() => (<Redirect to={`${match.url}/class-maintenance`} />)} />
          {
            routes.map(({ path, ...dynamics }, index) => (
              <Route
                key={index}
                exact
                path={path}
                component={dynamic({ app, ...dynamics })}
              />
            ))
          }
        </Switch>
      )
    }
    
    export default StepRoutes
    
  3. 2.然后,当切换到路线但Warning时,模型进行了动态加载。我怎么解决这个问题 ? the error

0 个答案:

没有答案