iT邦幫忙

0

React的路由功能:將Index.js的頁面,指派到Router.js,再由它去指派要渲染的元件。

1.將index.js的頁面,指派到Router.js

import React from 'react';
import ReactDOM from 'react-dom';
import Router from 'Router';

ReactDOM.render(<Router />,document.getElementById('root'))

2.再由Router.js去指派要呼叫那個元件
Router.js

import App from 'pages/App.js'
import Login from 'pages/login.js'
import NotFound from 'pages/NotFound'
import {BrowserRouter,Switch,Route} from 'react-router-dom'

const Router =()=>(
    <BrowserRouter>
		<Switch>
			<Route path='/' exact component={App} /> 
            <Route path='/login' component={Login} />
            <Route component={NotFound}/>                //找不到就用這一個
        </Switch>
    </BrowserRouter>
)	   		
export default Router;

尚未有邦友留言

立即登入留言