码迷,mamicode.com
首页 > 其他好文 > 详细

React 路由基本配置

时间:2020-03-15 09:22:58      阅读:46      评论:0      收藏:0      [点我收藏+]

标签:xtend   product   router   官方文档   assets   内容   import   rom   路由   

app.js

/*

  react路由的配置:
    1、找到官方文档 https://reacttraining.com/react-router/web/example/basic

    2、安装  cnpm install react-router-dom --save


    3、找到项目的根组件引入react-router-dom

       import { BrowserRouter as Router, Route, Link } from "react-router-dom";

    4、复制官网文档根组件里面的内容进行修改  (加载的组件要提前引入)


         <Router>

                <Link to="/">首页</Link>

                <Link to="/news">新闻</Link>

                <Link to="/product">商品</Link>


               <Route exact path="/" component={Home} />
               <Route path="/news" component={News} />    
               <Route path="/product" component={Product} />   
         </Router>


         exact表示严格匹配
         
*/



import React, { Component } from ‘react‘;

import { BrowserRouter as Router, Route, Link } from "react-router-dom";


import ‘./assets/css/index.css‘

import Home from ‘./components/Home‘;
import News from ‘./components/News‘;
import Product from ‘./components/Product‘;

class App extends Component {

  render() {
    return (
        <Router>
          <div>           

              <header className="title">
              
                <Link to="/">首页</Link>

                <Link to="/news">新闻</Link>

                <Link to="/product">商品</Link>

              </header>


               <br />
               <hr />
      
               <br />
      
      
              <Route exact path="/" component={Home} />
              <Route path="/news" component={News} />    
              <Route path="/product" component={Product} />                 
          </div>
      </Router>
    );
  }
}

export default App;

 

React 路由基本配置

标签:xtend   product   router   官方文档   assets   内容   import   rom   路由   

原文地址:https://www.cnblogs.com/loaderman/p/11556639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!