1
1
// 如何实现一个react-router路由拦截(导航守卫) https://juejin.im/post/5c31aede6fb9a04a0d570107
2
- import React from ' react' ;
3
- import { BrowserRouter , HashRouter , Route , Link , Switch } from ' react-router-dom' ;
2
+ import React from " react" ;
3
+ import { HashRouter , Route , Link , Switch } from " react-router-dom" ;
4
4
// import { browserHistory, Router} from 'react-router';
5
- import {
6
- Modal , message ,
7
- } from 'antd' ;
8
- import Home from '../page/CssTest/index' ;
9
- import history from '../history' ;
10
- import Counter from '../page/Counter/index' ;
11
- import Optimize from '../page/Optimize/index' ;
12
- import EchartBox from '../page/echartBox/index' ;
13
- import ChinaMap from '../page/chinaMap/index' ;
14
- import UseMemo from '../page/useMemo/index' ;
15
- import UseCallback from '../page/useCallback/index' ;
16
- import Memo from '../page/memoParent/index' ;
17
- import HeightEchart from '../page/heightEchart/index' ;
18
- import FormBox from '../page/form/index' ;
19
- import PromiseTest from '../page/promise/index' ;
5
+ import { Modal , message } from "antd" ;
6
+ import PropTypes from "prop-types" ;
7
+ import Home from "../page/CssTest/index" ;
8
+ import history from "../history" ;
9
+ import Counter from "../page/Counter/index" ;
10
+ import Optimize from "../page/Optimize/index" ;
11
+ import EchartBox from "../page/echartBox/index" ;
12
+ import ChinaMap from "../page/chinaMap/index" ;
13
+ import UseMemo from "../page/useMemo/index" ;
14
+ import UseCallback from "../page/useCallback/index" ;
15
+ import Memo from "../page/memoParent/index" ;
16
+ import HeightEchart from "../page/heightEchart/index" ;
17
+ import FormBox from "../page/form/index" ;
18
+ import PromiseTest from "../page/promise/index" ;
20
19
21
20
/**
22
21
* 解决ant-design的路由跳转,modal和message组件不关闭问题
@@ -35,7 +34,7 @@ function Header() {
35
34
echart
36
35
< ul >
37
36
< li >
38
- < Link to = "/echart" > echart的简单应用</ Link >
37
+ < Link to = "/echart" > echart的简单应用</ Link >
39
38
</ li >
40
39
< li >
41
40
< Link to = "/map" > echart实现中国地图并实现图层叠加</ Link >
@@ -49,31 +48,57 @@ function Header() {
49
48
react-router(可打开控制台观察数据的变化)
50
49
< ul >
51
50
< li >
52
- < Link to = { { pathname :"/query" , query : { name : 'Anny' , time : '2' , obj : JSON . stringify ( {
53
- name : 'IG' ,
54
- sex : 'man' ,
55
- } ) } } } > 路由query传参</ Link >
51
+ < Link
52
+ to = { {
53
+ pathname : "/query" ,
54
+ query : {
55
+ name : "Anny" ,
56
+ time : "2" ,
57
+ obj : JSON . stringify ( {
58
+ name : "IG" ,
59
+ sex : "man"
60
+ } )
61
+ }
62
+ } }
63
+ >
64
+ 路由query传参
65
+ </ Link >
56
66
</ li >
57
67
< li >
58
- < Link to = { `/link?data=${ encodeURIComponent ( JSON . stringify ( {
59
- name : 'IG' ,
60
- sex : 'man' ,
61
- } ) ) } `} >
68
+ < Link
69
+ to = { `/link?data=${ encodeURIComponent (
70
+ JSON . stringify ( {
71
+ name : "IG" ,
72
+ sex : "man"
73
+ } )
74
+ ) } `}
75
+ >
62
76
路由Link to传一个对象,并url加密
63
77
</ Link >
64
78
</ li >
65
79
< li >
66
- < Link to = { { pathname :"/params" , params : { name : 'Ming' , year :'18' , obj : JSON . stringify ( {
67
- name : 'IG' ,
68
- sex : 'man' ,
69
- } ) } } } > 路由params传参</ Link >
80
+ < Link
81
+ to = { {
82
+ pathname : "/params" ,
83
+ params : {
84
+ name : "Ming" ,
85
+ year : "18" ,
86
+ obj : JSON . stringify ( {
87
+ name : "IG" ,
88
+ sex : "man"
89
+ } )
90
+ }
91
+ } }
92
+ >
93
+ 路由params传参
94
+ </ Link >
70
95
</ li >
71
96
< li >
72
97
< Link to = "/queryChildren" > 嵌套路由</ Link >
73
98
</ li >
74
99
</ ul >
75
100
</ li >
76
-
101
+
77
102
< li >
78
103
< Link to = "/counter" > react-redux的应用和异步流</ Link >
79
104
</ li >
@@ -99,66 +124,39 @@ function Header() {
99
124
) ;
100
125
}
101
126
102
- function App ( ) {
103
- return (
104
- // basename 为路由前添加前缀
105
- // <BrowserRouter basename={'/douyu'}>
106
- < HashRouter >
107
- < div style = { { marginLeft : 40 } } >
108
- < Header />
109
- < Switch >
110
- < Route exact path = "/" component = { Home } />
111
- < Route path = "/query" component = { RouteQuery } />
112
- < Route path = "/link" component = { RouteLink } />
113
- < Route path = "/params" component = { RouteParams } />
114
- < Route path = "/queryChildren" component = { QueryChildren } />
115
- < Route path = "/counter" component = { Counter } />
116
- < Route path = "/optimize" component = { Optimize } />
117
- < Route path = "/echart" component = { EchartBox } />
118
- < Route path = "/map" component = { ChinaMap } />
119
- < Route path = "/useMemo" component = { UseMemo } />
120
- < Route path = "/useCallback" component = { UseCallback } />
121
- < Route path = "/memo" component = { Memo } />
122
- < Route path = "/heightEchart" component = { HeightEchart } />
123
- < Route path = "/form" component = { FormBox } />
124
- < Route path = "/testPromise" component = { PromiseTest } />
125
- </ Switch >
126
- </ div >
127
- </ HashRouter >
128
- ) ;
129
- }
130
-
131
127
function RouteLink ( { location } ) {
132
- console . log ( JSON . parse ( new URLSearchParams ( location . search ) . get ( ' data' ) ) ) ;
133
- if ( ! location . search ) {
134
- return ' No Click' ;
128
+ console . log ( JSON . parse ( new URLSearchParams ( location . search ) . get ( " data" ) ) ) ;
129
+ if ( ! location . search ) {
130
+ return " No Click" ;
135
131
}
136
- const data = JSON . parse ( new URLSearchParams ( location . search ) . get ( ' data' ) ) ;
137
- return Object . keys ( data ) . map ( ( item ) => (
132
+ const data = JSON . parse ( new URLSearchParams ( location . search ) . get ( " data" ) ) ;
133
+ return Object . keys ( data ) . map ( item => (
138
134
< div key = { item } >
139
135
{ item } : { data [ item ] }
140
136
</ div >
141
- ) )
137
+ ) ) ;
142
138
}
143
139
144
140
function RouteQuery ( { location } ) {
145
- console . log ( ' query传参' , location ) ;
146
- return ( < h2 > About: { location . query ? `name:${ location . query . name } , time: ${ location . query . time } ` : ' No Click' } </ h2 > ) ;
141
+ console . log ( " query传参" , location ) ;
142
+ return < h2 > About: { location . query ? `name:${ location . query . name } , time: ${ location . query . time } ` : " No Click" } </ h2 > ;
147
143
}
148
144
149
- function RouteParams ( { location} ) {
150
- console . log ( 'params传参' , location ) ;
151
- return ( < h2 > 试一下: { location . params ? `name为${ location . params . name } , year为${ location . params . year } ` : 'No Click' } </ h2 > ) ;
145
+ function RouteParams ( { location } ) {
146
+ console . log ( "params传参" , location ) ;
147
+ return (
148
+ < h2 > 试一下: { location . params ? `name为${ location . params . name } , year为${ location . params . year } ` : "No Click" } </ h2 >
149
+ ) ;
152
150
}
153
151
154
-
155
152
function SonQuery ( { location } ) {
156
- console . log ( ' 嵌套路由' , location ) ;
153
+ console . log ( " 嵌套路由" , location ) ;
157
154
if ( location . query ) {
158
155
return location . query . name ;
159
- } else if ( location . params ) {
156
+ }
157
+ if ( location . params ) {
160
158
return location . params . name ;
161
- }
159
+ }
162
160
return < h3 > No Click</ h3 > ;
163
161
}
164
162
@@ -168,10 +166,10 @@ function QueryChildren({ match }) {
168
166
< h2 > 嵌套路由</ h2 >
169
167
< ul >
170
168
< li >
171
- < Link to = { { pathname :`${ match . path } /apple` , query : { name : '苹果' } } } > 苹果</ Link >
169
+ < Link to = { { pathname : `${ match . path } /apple` , query : { name : "苹果" } } } > 苹果</ Link >
172
170
</ li >
173
171
< li >
174
- < Link to = { { pathname :`${ match . path } /putao` , params : { name : '葡萄' } } } > 葡萄</ Link >
172
+ < Link to = { { pathname : `${ match . path } /putao` , params : { name : "葡萄" } } } > 葡萄</ Link >
175
173
</ li >
176
174
</ ul >
177
175
< Switch >
@@ -182,4 +180,45 @@ function QueryChildren({ match }) {
182
180
) ;
183
181
}
184
182
183
+ function App ( ) {
184
+ return (
185
+ // basename 为路由前添加前缀
186
+ // <BrowserRouter basename={'/douyu'}>
187
+ < HashRouter >
188
+ < div style = { { marginLeft : 40 } } >
189
+ < Header />
190
+ < Switch >
191
+ < Route exact path = "/" component = { Home } />
192
+ < Route path = "/query" component = { RouteQuery } />
193
+ < Route path = "/link" component = { RouteLink } />
194
+ < Route path = "/params" component = { RouteParams } />
195
+ < Route path = "/queryChildren" component = { QueryChildren } />
196
+ < Route path = "/counter" component = { Counter } />
197
+ < Route path = "/optimize" component = { Optimize } />
198
+ < Route path = "/echart" component = { EchartBox } />
199
+ < Route path = "/map" component = { ChinaMap } />
200
+ < Route path = "/useMemo" component = { UseMemo } />
201
+ < Route path = "/useCallback" component = { UseCallback } />
202
+ < Route path = "/memo" component = { Memo } />
203
+ < Route path = "/heightEchart" component = { HeightEchart } />
204
+ < Route path = "/form" component = { FormBox } />
205
+ < Route path = "/testPromise" component = { PromiseTest } />
206
+ </ Switch >
207
+ </ div >
208
+ </ HashRouter >
209
+ ) ;
210
+ }
211
+
212
+ RouteParams . propTypes = {
213
+ location : PropTypes . object . isRequired
214
+ } ;
215
+ SonQuery . propTypes = {
216
+ location : PropTypes . object . isRequired
217
+ } ;
218
+ RouteQuery . propTypes = {
219
+ location : PropTypes . object . isRequired
220
+ } ;
221
+ QueryChildren . propTypes = {
222
+ match : PropTypes . object . isRequired
223
+ } ;
185
224
export default App ;
0 commit comments