|
1 | 1 | import React from "react";
|
2 |
| -import { HashRouter ,BrowserRouter, Route, Link,Switch } from "react-router-dom"; |
| 2 | +import { BrowserRouter, Route, Link,Switch } from "react-router-dom"; |
3 | 3 | import Home from '../page/CssTest/index'
|
| 4 | +// import About from '../page/About/index' |
| 5 | + |
4 | 6 | import Counter from '../page/Counter/index'
|
5 | 7 | import Single from '../page/Single/index'
|
6 | 8 |
|
7 |
| - |
| 9 | +function Header() { |
| 10 | + return ( |
| 11 | + <ul> |
| 12 | + <li> |
| 13 | + <Link to="/">css避免全局渲染方案</Link> |
| 14 | + </li> |
| 15 | + <li> |
| 16 | + <Link to={{ pathname:"/about" , query: { name: 'Anny' } }}>路由query传单个属性</Link> |
| 17 | + </li> |
| 18 | + <li> |
| 19 | + <Link to={`/practice?data=${encodeURIComponent(JSON.stringify({ |
| 20 | + name: 'IG', |
| 21 | + sex: 'man', |
| 22 | + }))}`} > |
| 23 | + 路由query传一个对象 |
| 24 | + </Link> |
| 25 | + </li> |
| 26 | + <li> |
| 27 | + <Link to={{ pathname:"/test" , params: { name: 'Ming' } }}>路由params传参</Link> |
| 28 | + </li> |
| 29 | + <li> |
| 30 | + <Link to="/queryChildren">嵌套路由</Link> |
| 31 | + </li> |
| 32 | + <li> |
| 33 | + <Link to="/counter">全局状态共享计数器操作</Link> |
| 34 | + </li> |
| 35 | + <li> |
| 36 | + <Link to="/single">计数器显示</Link> |
| 37 | + </li> |
| 38 | + </ul> |
| 39 | + ); |
| 40 | +} |
8 | 41 |
|
9 | 42 | function App() {
|
10 |
| - // const [count, setCount] = useState(0); |
11 |
| - |
12 | 43 | return (
|
13 |
| - <HashRouter> |
| 44 | + // basename 为路由前添加前缀 |
| 45 | + // <BrowserRouter basename={'/douyu'}> |
| 46 | + <BrowserRouter> |
14 | 47 | <div>
|
15 | 48 | <Header />
|
16 |
| - <div> |
| 49 | + <Switch> |
17 | 50 | <Route exact path="/" component={Home} />
|
18 | 51 | <Route path="/about" component={About} />
|
19 |
| - <Route path="/hasChildren" component={HasChildren} /> |
| 52 | + <Route path="/practice" component={Practice} /> |
| 53 | + <Route path="/test" component={Test} /> |
| 54 | + <Route path="/queryChildren" component={QueryChildren} /> |
20 | 55 | <Route path="/counter" component={Counter} />
|
21 | 56 | <Route path="/single" component={Single} />
|
22 |
| - </div> |
23 |
| - |
| 57 | + </Switch> |
24 | 58 | </div>
|
25 |
| - </HashRouter> |
| 59 | + </BrowserRouter> |
26 | 60 | );
|
27 | 61 | }
|
28 | 62 |
|
| 63 | +function Practice({ location }) { |
| 64 | + console.log(JSON.parse(new URLSearchParams(location.search).get('data'))); |
| 65 | + if (!location.search ) { |
| 66 | + return 'No Click'; |
| 67 | + } |
| 68 | + const data = JSON.parse(new URLSearchParams(location.search).get('data')); |
| 69 | + return Object.keys(data).map((item) => ( |
| 70 | + <div key={item}> |
| 71 | + {item} : {data[item]} |
| 72 | + </div> |
| 73 | + )) |
| 74 | +} |
| 75 | + |
| 76 | +function About({ location }) { |
| 77 | + return (<h2>About: { location.query ? location.query.name : 'No Click' }</h2>); |
| 78 | +} |
29 | 79 |
|
30 |
| -function About({location}) { |
31 |
| -console.log(location); |
32 |
| - return (<h2>About: { location.query ? location.query.name : 'no more' }</h2>); |
| 80 | +function Test({location}) { |
| 81 | + return (<h2>Test: {location.params ? location.params.name : 'No Click' }</h2>); |
33 | 82 | }
|
34 | 83 |
|
35 |
| -function ItemDetails() { |
36 |
| - return <h2>ItemDetails</h2>; |
37 |
| - } |
38 | 84 |
|
39 |
| -function Topic() { |
40 |
| - return <h3>Components</h3>; |
| 85 | +function SonQuery({ location }) { |
| 86 | + // console.log('SonQuery', location); |
| 87 | + if (location.query) { |
| 88 | + return location.query.name; |
| 89 | + } else if (location.params) { |
| 90 | + return location.params.name; |
| 91 | + } |
| 92 | + return <h3> No Click</h3>; |
41 | 93 | }
|
42 | 94 |
|
43 |
| -function HasChildren({ match }) { |
44 |
| - console.log(match); |
| 95 | +function QueryChildren({ match }) { |
45 | 96 | return (
|
46 | 97 | <div>
|
47 |
| - <h2>Topics</h2> |
48 |
| - |
| 98 | + <h2>嵌套路由</h2> |
49 | 99 | <ul>
|
50 | 100 | <li>
|
51 |
| - <Link to={`${match.path}/Components`}>Components</Link> |
| 101 | + <Link to={{ pathname:`${match.path}/apple` , query: { name: '苹果' } }}>苹果</Link> |
52 | 102 | </li>
|
53 | 103 | <li>
|
54 |
| - <Link to={`${match.path}/itemDetails`}>itemDetails</Link> |
| 104 | + <Link to={{ pathname:`${match.path}/putao`, params: { name: '葡萄' } }}>葡萄</Link> |
55 | 105 | </li>
|
56 | 106 | </ul>
|
57 |
| - |
58 |
| - {/* <Route path={`${match.path}/:id`} component={Topic} /> */} |
59 | 107 | <Switch>
|
60 |
| - <Route path='/hasChildren/ItemDetails' component={ItemDetails} /> |
61 |
| - <Route path='/hasChildren/' component={Topic} /> |
| 108 | + <Route path={`${match.path}/`} component={SonQuery} /> |
| 109 | + <Route path={`${match.path}/putao`} component={SonQuery} /> |
62 | 110 | </Switch>
|
63 | 111 | </div>
|
64 | 112 | );
|
65 | 113 | }
|
66 |
| - |
67 |
| -function Header() { |
68 |
| - return ( |
69 |
| - <ul> |
70 |
| - <li> |
71 |
| - <Link to="/">Home</Link> |
72 |
| - </li> |
73 |
| - <li> |
74 |
| - <Link to={{ pathname:"/about" , query: { name: 'Anny' } }}>About</Link> |
75 |
| - </li> |
76 |
| - <li> |
77 |
| - <Link to="/hasChildren">HasChildren</Link> |
78 |
| - </li> |
79 |
| - <li> |
80 |
| - <Link to="/counter">Counter</Link> |
81 |
| - </li> |
82 |
| - <li> |
83 |
| - <Link to="/single">single</Link> |
84 |
| - </li> |
85 |
| - </ul> |
86 |
| - ); |
87 |
| -} |
88 | 114 |
|
89 | 115 | export default App;
|
0 commit comments