dynamic(app, model, component)

    • app,挂载的对象,就是你要将这个router挂载到哪个实例上
    • model,这个router所需要的model
    • component,当前 router路由要加载的组件
    1. dynamic(app, model, component )
    2. import dynamic from "dva/dynamic";
    3. import app from "../app";
    4. export const UserPageDynamic = dynamic({
    5. app,
    6. models: () => [import("../models/user")],
    7. component: () => import("../routes/UserPage")
    8. });
    9. const routerConfig = {
    10. '/': {
    11. component: dynamicWrapper(app, ['user', 'login'], () => import('../layouts/BasicLayout')),
    12. },
    13. '/dashboard/analysis': {
    14. component: dynamicWrapper(app, ['chart'], () => import('../routes/Dashboard')),
    15. },
    16. }

    models: () => [import(“../models/user”)]
    为什么返回函数?
    让他先不执行,调用时才执行;如果不是函数,直接就执行了

    dva的三个方法
    app.model
    app.router
    app.start

    src/index.js

    1. import React from 'react'
    2. import dva, { connect } from 'dva'
    3. import { Router, Route } from 'dva/router'
    4. import dvaLoading from 'dva-loading'
    5. import keymaster from 'keymaster'
    6. import { createLogger } from 'redux-logger'
    7. import './index.less'
    8. const waitTime = (ms=1000) => new Promise(resolve => setTimeout(() => resolve(), ms))
    9. // 1 初始化 onAction = applyMiddleware(createLogger)
    10. const app = dva({
    11. onAction: createLogger(),
    12. })
    13. // 2. Plugins app.use()
    14. app.use(dvaLoading())
    15. /**
    16. 多个 model,等价于 combineReducer
    17. state = {
    18. counter: { number: 0 },
    19. list: { number: 100 }
    20. }
    21. */
    22. app.model({
    23. namespace: 'counter',
    24. state: { number: 0 },
    25. reducers: {
    26. // 属性名就是action-type,值就是一个函数,用来计算新状态的
    27. // store.dispatch({type:'counter/add'})
    28. add (state, action) {
    29. // action = {type:'counter/add', payload: {value: 100}}
    30. console.log('action', action)
    31. const { value } = action.payload
    32. return { number: state.number + value}
    33. },
    34. minus(state, action) {
    35. const { value } = action.payload
    36. return { number: state.number - value }
    37. },
    38. log(state) {
    39. console.log('reducers log')
    40. return { number: 100 }
    41. }
    42. },
    43. // 异步操作,都要放在 effects里面
    44. effects: {
    45. *asyncAdd(action, { put, call }) {
    46. yield call(waitTime)
    47. yield put({ type: 'add', payload: { value: 20 } }) // effect里面不需要写 namespace
    48. },
    49. *log(action, { select }) {
    50. let state = yield select(state => state.counter)
    51. console.log('effects log', state)
    52. }
    53. },
    54. subscriptions: {
    55. changeTitle({ history }) {
    56. history.listen((location) => {
    57. console.log(location)
    58. document.title = location.pathname
    59. })
    60. },
    61. keyboard({ dispatch }) {
    62. keymaster('space', () => {
    63. dispatch({ type: 'add' })
    64. })
    65. }
    66. }
    67. })
    68. // 可以定义多个 model
    69. app.model({
    70. namespace: 'list',
    71. state: { array: [100], number: 100 },
    72. reducers: {
    73. add(state, action) {
    74. const { array, number } = state
    75. const { value } = action.payload
    76. const newValue = number + value
    77. return { number: newValue, array: [...array, newValue] }
    78. },
    79. minus(state, {payload}) {
    80. const { array, number } = state
    81. const newValue = number - payload.value
    82. return { number: newValue, array: [...array, newValue] }
    83. }
    84. }
    85. })
    86. // model 声明数据处理文件位置
    87. // app.model(require('./models/list').default)
    88. // connect连接组件和 dva model
    89. const CounterConnect = connect(state => state.counter)(Counter)
    90. const ListConnect = connect(state => state.list)(List)
    91. // 4 挂载路由
    92. app.router(props => {
    93. const { history } = props
    94. console.log(history, 100)
    95. return (
    96. <Router history={history}>
    97. <>
    98. <Route path="/" component={CounterConnect}/>
    99. <Route path="/list" component={ListConnect}/>
    100. </>
    101. </Router>
    102. )
    103. })
    104. // app.router(require('./router').default)
    105. // app.start 启动项目
    106. app.start('#root')
    107. function Counter(props) {
    108. const { number, dispatch } = props
    109. const addAction = { type: 'counter/add', payload: { value: 100 } }
    110. const minusAction = { type: 'counter/minus', payload: { value: 30 } }
    111. return (
    112. <div>
    113. <h2>{number}</h2>
    114. <button onClick={() => dispatch(addAction)}>增加 100</button>
    115. <button onClick={() => dispatch(minusAction)}>减少 30</button>
    116. <hr/>
    117. <button onClick={() => dispatch({ type: 'counter/asyncAdd', payload: {value: 20}})}>async异步加 20</button>
    118. <button onClick={() => dispatch({ type: 'counter/logger' })}>console日志</button>
    119. </div>
    120. )
    121. }
    122. function List(props) {
    123. const { number, array, dispatch } = props
    124. const addAction = { type: 'list/add', payload: { value: 10 } }
    125. const minusAction = { type: 'list/minus', payload: { value: 1 } }
    126. return (
    127. <div>
    128. <h2>{ number }</h2>
    129. <p>{ String(array) }</p>
    130. <button onClick={() => dispatch(addAction) }>增加 10</button>
    131. <button onClick={() => dispatch(minusAction) }>减少 1</button>
    132. </div>
    133. )
    134. }