React dva/dynamic 参数

Posted liujiaxun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React dva/dynamic 参数相关的知识,希望对你有一定的参考价值。

 
 1 import dynamic from ‘dva/dynamic‘;
 2 import { Route, Switch, Redirect } from ‘dva/router‘;
 3 export const newDynamic = (app,model,component) => {
 4     // console.log(app,model,component);
 5     return dynamic({
 6         app,
 7         models:() => model,
 8         component
 9     })
10 }
11 /**
12  * @param {app} app
13  * @param {func} routerConfig 
14  */
15 export const createRouters = (app,routerConfig) => {
16     return (
17         <Switch>
18             {
19                 routerConfig(app).map( config => createRouter( app,() => config ))
20             }
21         </Switch>
22     )
23 
24 }
25 
26 export const createRouter = (app,routerConfig) => {
27     const { 
28         title,
29         path,
30         indexRouter,
31         component:Comp,
32         ...otherProps
33     } = routerConfig(app);
34     if (path && path !== ‘/‘) {
35         window.dva_router_pathMap[path] = { path, title, ...otherProps };
36     }
37     if(path === ‘/login‘){
38         console.log(routerConfig(app).component,‘routerConfig‘);
39     }
40     const routePorps = Object.assign({
41         key:path || ‘/404‘,
42         render:props => <Comp routerData={otherProps} {...props} />
43     },
44     path && {
45       path: path
46     });
47     if(path === ‘/login‘){
48         console.log(routePorps,‘/sign/login‘)
49       }
50     if(indexRouter){
51         return [
52             <Redirect key={path + ‘_redirect‘} exact from={path} to={indexRouter} />,
53             <Route {...routePorps} />
54           ];
55     }
56     return <Route {...routePorps} />;
57 }

生成动态dva路由,其中 dynamic  需要注意 里面json参数顺序,app , models , component

以上是关于React dva/dynamic 参数的主要内容,如果未能解决你的问题,请参考以下文章

javascript 用于在节点#nodejs #javascript内设置react app的代码片段

前端开发工具vscode如何快速生成代码片段

前端开发工具vscode如何快速生成代码片段

[React Testing] Use Generated Data in Tests with tests-data-bot to Improve Test Maintainability(代码片段

react简介

react简介