17. react-router-dom

1.  路由传参: https://www.cnblogs.com/cckui/p/11490372.html

2.  代码:

复制代码
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
import './index.css';

import { BrowserRouter, HashRouter, Route, Redirect, Switch,
  Link, NavLink
} from 'react-router-dom'

function Home (props) {
  return (
    <Fragment>
      <h3>home...........</h3>
      <Link to={{
        pathname: '/detail',
        hash: '#d',
        search: '?id=001',
        state: {
          id: '001'
        }
      }}>
        跳转到详情页
      </Link>
    </Fragment>
  )
}

function Login (props) {
  return <h3>Login...........</h3>
}

function Detail (props) {
  console.log(props, "sss")
  return <h3>Detail...........</h3>
}

function NoMatch (props) {
  return <h3>NoMatch...........</h3>
}

class Nav extends React.Component {
  constructor (props) {
    super(props)
  }

  render () {
    return (
        <>
          <NavLink
            to="/login"
            activeStyle={{
              fontWeight: 'bold',
              color: 'red'
            }}
          >
            <span>登录</span>
          </NavLink>

          <NavLink
            to="/home"
            activeStyle={{
              fontWeight: 'bold',
              color: 'red'
            }}
          >
            <span>home</span>
          </NavLink>
        </>
        
    )
  }
}

class App extends React.Component {
  render() {
    return (
      <div>
        <BrowserRouter>
          <Nav />
          <Switch>
            <Route path='/' exact component={Home}></Route>
            <Route path='/home' exact component={Home}></Route>
            <Route path='/login' exact component={Login}></Route>
            <Route path='/detail' exact component={Detail}></Route>
            <Route path='/detail/:id' component={Detail}></Route>
            <Route path='/404' exact component={NoMatch}></Route>
            <Redirect from="/" to="/404"></Redirect>
          </Switch>
        </BrowserRouter>
      </div>
      
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById("root")
)
复制代码

 

posted @   monkey-K  阅读(179)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示