• react 使用和封装路由(router.js)


    使用route 之前需要先安装 react-router-dom

    yarn add react-router-dom -D

    在src根目录下新建router.js文件

    //router.js
    import React,{ Component } from 'react' import {Route, BrowserRouter, Switch, HashRouter} from 'react-router-dom' import Earnings from './page/earnings/earnings' import Home from './page/home/home' import UserVip from './page/userVip/userVip' export default class Router extends Component { render(){ return ( // 建议使用 HashRouter <HashRouter> <Switch>
                // exact 精准匹配 <Route path="/" exact component={ UserVip }></Route> <Route path="/earnings" component={ Earnings } /> <Route path="/home" component={ Home } />
                <Route component={ 如果前面的路由都没有匹配上,在此处返回404页面 } /> </Switch> </HashRouter> ) } }

     第二种写法  

    import {Route, BrowserRouter, Switch, HashRouter} from 'react-router-dom'
    import Earnings from './page/earnings/earnings'
    import Home from './page/home/home'
    import UserVip from './page/userVip/userVip'

    export default function Router() {
        return (
            <HashRouter>
                <Switch>
                    <Route path="/" exact component={ UserVip }></Route>
                    <Route path="/earnings" component={ Earnings } />
                    <Route path="/home" component={ Home } />
                    <Route component={ 如果前面的路由都没有匹配上,在此处返回404页面 } />
                </Switch>
            </HashRouter>
        )
    }

    使用 在index.js中引入router.js

    import React from 'react';
    import ReactDOM from 'react-dom';
    import Router from './router'
    
    ReactDOM.render(
      <Router></Router>
    ,document.getElementById('root'));
  • 相关阅读:
    Code First 数据迁移 转
    WebAPI Post接收数据
    hbase1.2.4 API改动
    spark中RDD持久化浅析
    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Attr_id' in 'where clause'
    宝塔忘记面板地址怎么办? 可以找到没问题的
    卸载Apache 删除已经停止的服务
    Composer 卸载
    TP5.1模型 增删改查
    打印数据库int类型的时间戳
  • 原文地址:https://www.cnblogs.com/tlfe/p/14759574.html
Copyright © 2020-2023  润新知