【问题标题】:React router not returning dynamic path反应路由器不返回动态路径
【发布时间】:2016-09-08 18:52:38
【问题描述】:

我正在使用带有这些路由的反应路由器:

let routes = [{
  path : homePath,
  component : Layout,
  indexRoute: { component : Home },
  childRoutes : [
    {
      path : "translations",
      component : Translation,
    },
    {
      path : "articles",
      component : Articles,
    },{
      path : "article/:id",
      component : Article,
    },
    {
      path : "contact",
      component : Contact,
    },
  ],
}];
ReactDOM.render(
  <Router routes = {routes} history = {rRouter.browserHistory}>
  </Router>
  ,app);

组件:

class Articles extends React.Component{
  render(){
    return(
      <h1>Articles</h1>
    );
  }
}
class Article extends React.Component{
  render(){
    console.log(this.props);
    return(
      <h1>Article</h1>
    );
  }
}

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name=viewport content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>HYassin</title>
    <link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'/>
    <link href='http://yui.yahooapis.com/pure/0.6.0/pure-min.css' rel='stylesheet' type='text/css'/>
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="http://localhost/Hyassin/src/css/main.css" rel="stylesheet" type="text/css"/>
    <!--  React stuff  -->
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js"></script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/react-router/2.7.0/ReactRouter.js"></script>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/superagent/2.2.0/superagent.js"></script>
    <script src = "https://unpkg.com/mobx@2.5.1/lib/mobx.umd.js"></script>
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>

  <body>
    <div id = "webapp"></div>
    <script src = "./src/js/bundle.js"></script>
  </body>
</html>

.htaccess

RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]
  RewriteRule ^ /index.php [L]

如果我尝试转到路径 localhost/articles 它工作正常,但是如果我尝试使用 localhost/article/1 它会给我以下错误Uncaught SyntaxError: Unexpected token &lt;.Components 和路由在同一个 js 文件中。此错误的原因可能是什么?感谢您的宝贵时间。

【问题讨论】:

  • 听起来像是在期待 Javascript 但接收到 HTML - 检查您的网络标签
  • 能否提供快递路线,以及index.html
  • 您是否在开发服务器上设置了 URL 后备?即使尝试访问开发服务器上的未知路由,您是否还在为您的index.html 提供服务?
  • 我已经找到了我的问题的答案

标签: javascript reactjs react-router


【解决方案1】:

原来问题是由我包含脚本文件的方式引起的。我不得不更改为src = "/src/js/bundle.js" 而不是src ="http://localhost/src/js/bundle.js"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 2022-01-17
    • 2017-12-11
    • 2018-03-08
    • 2016-02-24
    • 1970-01-01
    • 2017-04-05
    相关资源
    最近更新 更多