【React】react配置路由

gengboxb 656 0

1、安装依赖

yarn add react-router-dom

2、创建路由文件router.js

import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import login from '../pages/login/login';

class RouteConfig extends Component {
  render() {
    return (
      <Router>
        {/* 配置首页 */}
        <Route path="/" exact component={login} />
        <Route path="/login/" component={login} />
      </Router>
    );
  }
}

export default RouteConfig;

3、index.js引入router.js

import React from 'react';
import ReactDOM from 'react-dom';
import Route from './router/router';
import * as serviceWorker from './serviceWorker';

const render = (Component) => {
  ReactDOM.render(
    <Component />,
    document.getElementById('root'),
  );
};

render(Route);
serviceWorker.unregister();

发表评论 取消回复
表情 图片 链接 代码

分享