React动态路由

import React from 'react';
import './App.css';
import {BrowserRouter as Router,Switch,Route,Link} from 'react-router-dom'

function Home() {
    return(
        
to About

Home

) } function About() { return(
to Home

About

) } function News(props) { return(

当前打开的是ID:{props.match.params.id}的新闻

) } function NotFound(props) { let toIndex = () => { props.history.push('/') } return(

你访问的页面{props.location.pathname}找不到

) } function App() { return(
) } export default App;

1.安装依赖npm install react-router-dom
2.导入路由 import {BrowserRouter as Router,Switch,Route,Link} from 'react-router-dom'

路由跳转:

  • to About
  • props.history.push('/')

你可能感兴趣的:(React动态路由)