在React-Router中没有获取组件的道具

时间:2018-07-11 08:20:37

标签: react-router react-router-v4 react-router-component

我想使用React Component来获取ID匹配,但是在转到url时会出错:http://localhost:8080/portfolio/1并在其他url上运行良好。错误:

GET http://localhost:8080/portfolio/bundle.js 404 (Not Found)      1:1  
Refused to execute script from 'http://localhost:8080/portfolio/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

AppRouter.js

  import React from 'react';
  import { BrowserRouter, Route, Switch, Link,NavLink } from 'react-router-dom';
  import Home from '../Components/Home';
  import Contact from '../Components/Contact';
  import PortfolioItemPage from '../Components/PortfolioItemPage.js'; 
  import NotFound from '../Components/NotFound.js';
  import Header from '../Components/Header';


  const AppRouter = () => (
 <BrowserRouter>
   <div>

     <Header/>
     <Switch>
         <Route path="/" component={Home} exact={true} />
         <Route path="/portfolio/:id" component={PortfolioItemPage} />

         <Route path="/contact" component={Contact}/>
         <Route component={NotFound} />
     </Switch>
   </div>
 </BrowserRouter>
);

  export default AppRouter;

Home Page

PortfolioItemPage.js

  import React from 'react';
  const PortfolioItemPage = (props) => {
  console.log(props);
    return (
   <div>
     <h1>Hii there...</h1>
    </div>
   );
   };
     export default PortfolioItemPage;

该怎么做才能使我在进入http://localhost:8080/portfolio/1时不会出错?

1 个答案:

答案 0 :(得分:0)

好像您的应用尝试从相对路径bundle.js加载http://localhost:8080/portfolio/

尝试将<base href="/" />添加到<html><head>中,以从根URL强制应用加载资源。