Skip to content

Commit 2c0caa0

Browse files
committed
Router学习
1 parent 771b953 commit 2c0caa0

File tree

2 files changed

+85
-50
lines changed

2 files changed

+85
-50
lines changed

src/page/About/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 验证路由传参
2+
3+
import React from 'react';
4+
5+
function Page({location}) {
6+
return (<h2>About: { location.query ? location.query.name : 'No Click' }</h2>);
7+
}
8+
9+
export default Page;

src/router/index.js

Lines changed: 76 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,115 @@
11
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";
33
import Home from '../page/CssTest/index'
4+
// import About from '../page/About/index'
5+
46
import Counter from '../page/Counter/index'
57
import Single from '../page/Single/index'
68

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+
}
841

942
function App() {
10-
// const [count, setCount] = useState(0);
11-
1243
return (
13-
<HashRouter>
44+
// basename 为路由前添加前缀
45+
// <BrowserRouter basename={'/douyu'}>
46+
<BrowserRouter>
1447
<div>
1548
<Header />
16-
<div>
49+
<Switch>
1750
<Route exact path="/" component={Home} />
1851
<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} />
2055
<Route path="/counter" component={Counter} />
2156
<Route path="/single" component={Single} />
22-
</div>
23-
57+
</Switch>
2458
</div>
25-
</HashRouter>
59+
</BrowserRouter>
2660
);
2761
}
2862

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+
}
2979

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>);
3382
}
3483

35-
function ItemDetails() {
36-
return <h2>ItemDetails</h2>;
37-
}
3884

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>;
4193
}
4294

43-
function HasChildren({ match }) {
44-
console.log(match);
95+
function QueryChildren({ match }) {
4596
return (
4697
<div>
47-
<h2>Topics</h2>
48-
98+
<h2>嵌套路由</h2>
4999
<ul>
50100
<li>
51-
<Link to={`${match.path}/Components`}>Components</Link>
101+
<Link to={{ pathname:`${match.path}/apple` , query: { name: '苹果' } }}>苹果</Link>
52102
</li>
53103
<li>
54-
<Link to={`${match.path}/itemDetails`}>itemDetails</Link>
104+
<Link to={{ pathname:`${match.path}/putao`, params: { name: '葡萄' } }}>葡萄</Link>
55105
</li>
56106
</ul>
57-
58-
{/* <Route path={`${match.path}/:id`} component={Topic} /> */}
59107
<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} />
62110
</Switch>
63111
</div>
64112
);
65113
}
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-
}
88114

89115
export default App;

0 commit comments

Comments
 (0)