app.get('/', function (req, res) {
res.send('hello world');
})
app.get('/hello', function (req, res) {
res.send('你好');
})
app.get('/hello/:name', function (req, res) {
res.send('你好, ' + req.params.name);
})
<Switch>
<Route path="/evaluations/:evaluationId/presentation" render={() => null} />
<Route path="/cases/:programId" render={() => null} />
<Route path="/cases/:programId/recusals/:recusalIndex/print" render={() => null} />
<Route component={SiteHeader} />
</Switch>
click <Link />
switch URL
render component
沒啥特別 => 看
function App() {
return (
<Router>
<div>
<Header />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
</div>
</Router>
);
}
你在的頁面
想要切換渲染的 component
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about/">About</Link>
</li>
<li>
<Link to="/users/">Users</Link>
</li>
</ul>
</nav>
link 提供一個
超連結到對應的
網址
click <Link />
switch URL
render component
隨著需要的頁面越多,變化開始有越來越多對應的網址
例如:部落格的文章頁面,商城的商品頁
那怎麼能一次把這些需求寫死吧?
因此,我們要用 “參數” 來指定 routing 的規則。
<Route path="/posts/:id?" component={PostRender} / >
因此子層的 component會這樣寫
function ProgramDetailPage({
match: {
params: {
programId,
},
},
}: ContextRouter) {
return (
<div style={styles.container}>
<Query
variables={{
id: programId,
}}
query={SCHEDULED_PROGRAM_DETAIL}>
{({ loading }) => {
if (loading) return <LoadingSpinner />;
return (
...
);
}
When true, will only match if the path matches the location.pathname exactly.
// 列舉所有文章
<Route exact path="/posts" component={Posts} />
// 展示某篇文章
<Route path="/posts/:id?" component={PostRender} />