反应路由器不能GET

时间:2017-12-10 02:06:52

标签: javascript reactjs react-router

我第一次尝试设置路由以做出反应。但事情并没有增加。

我收到消息:输入时无法GET /监听 “http://localhost:8080/monitors

App.js

import React from 'react';
import ReactDom from 'react-dom';
import Sidebar from './components/Sidebar'
import Main from './components/Main'

const App = () => {
      return (
        <div className="container">
            <Sidebar/>
            <Main/>
        </div>
      );
   }
export default App;

Main.js

import {Switch, Route} from 'react-router-dom'
import ProductsDisplay from './ProductsDisplay'
import Home from './Home'
import Contact from './Contact'

const Main = () => {
    <main>
        <Switch>
            <Route exact path="/" component={Home}/>
            <Route path="/contact" component={Contact}/>
            <Route path="/monitors" component={ProductsDisplay} category="monitors"/>
            <Route path="/machines" component={ProductsDisplay} category="machines"/>
        </Switch>
    </main>
}

ProductsDisplay.js

import React from 'react';
import ReactDom from 'react-dom';
import Product from './Product';

class ProductsDisplay extends React.Component {
    state = {
        productList: [],
        productCategory:''
    };

    constructor(props){
        super();
        productCategory = props.category;
    }

    componentDidMount() {
        this.setState(prevState => ({
            productList: [
                {productName: "ASUS 4K, 144Hz Monitor", productDescription: "Blah Blah Blah, cool stuff", productAvailability: true, productRating: 4.4},
                {productName: "Samsung 4K, 75Hz Monitor", productDescription: "Samsung cool stuff!", productAvailability: false, productRating: 4.0},
                {productName: "LG 2560x1080 Monitor, 60Hz Monitor", productDescription: "LG cool features!", productAvailability: false, productRating: 5.0},
            ]
        }));
    }

    render() {
        return (
            <div className="product-display">
            {this.state.productList.map((product, i) => 
                <Product productName={product.productName} 
                    productDescription={product.productDescription} 
                    productAvailability={product.productAvailability} 
                    productRating={product.productRating} key={i}/>
            )}
            </div>
        );
    }
}
export default ProductsDisplay;

当我渲染<App/>时,我用<BrowserRouter>围绕它我试图删除这些标记,但它没有效果。

1 个答案:

答案 0 :(得分:1)

您需要使用BrowserRouter

包装您的路线
import { BrowserRouter, Switch, Route } from 'react-router-dom';
<BrowserRouter>
      <Switch>
        <Route exact path="/" component={Fakebook}/>
        <Route exact path="/Home" component={Home}/>
        <Route component={NotFound}/>
      </Switch>
  </BrowserRouter>