【问题标题】:Warning: [react-router] Location "/add" did not match any routes警告:[react-router] 位置“/add”不匹配任何路由
【发布时间】:2019-05-06 11:38:12
【问题描述】:

我被 react-router 路由卡住了。我收到错误: 警告:[react-router] 位置“/add”不匹配任何路由` // conf.js

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link} from "react-router-dom";
import add  from './add';
import { createBrowserHistory } from 'history';
import confv1 from './confv1';
var button =React.createElement(Link, {
  to: "/add"
}, React.createElement("button", {
  type: "button"
}, "Add a project"));

  export default class Root extends Component {
    render() {
      return (


React.createElement(Router, {
  history: createBrowserHistory()
}, React.createElement(Route, {
  path: "/conf",
  component: confv1
}, React.createElement(Route, {
  component: conf
}), React.createElement(Route, {
  path: "/add",
  component: add
})
)));
      // );this is the conf page

and this is the add page when I refresh it I got the error "Warning: [react-router] Location "/add" did not match any routes`" } }

`

【问题讨论】:

  • 嗨,你能分享你的代码吗?
  • 你能帮帮我吗,拜托!!
  • @oumaimatirar 你使用哪个版本的 react-router-dom?请注意,您的带有“添加”路径的 Route 元素是带有“conf”路径的 Route 元素的子元素
  • "react-router-dom": "^5.0.0",
  • 我不知道如何解决这个问题,请帮忙?我不知道为什么当我刷新添加页面时,我得到了这个错误。到底是什么问题

标签: react-router


【解决方案1】:

我用 routes.js 解决了这个错误

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './App';
import conf from './conf';
import add from './add';


export default (
    <Route path="/" component={App}>
        <IndexRoute component={conf} />
        <Route path="/conf" component={conf} >
        <IndexRoute component={add} />
        <Route path="/add" component={add} />
           </Route>

    </Route>

);

//index.js

import 'babel-polyfill';
import React from 'react';

import {render} from 'react-dom';
import {Router, BrowserRouter } from 'react-router';
import routes from './routes';
import {browserHistory} from 'react-router';

const history = require("history").createBrowserHistory();


render(
    <Router history={browserHistory} routes={routes} />, document.getElementById('root')
)

但是当我刷新添加页面时,内容中没有显示任何内容enter image description here

【讨论】:

    【解决方案2】:

    我从您的代码 sn-p 中注意到您正在使用两个不同的 npm 包,我想知道这是否是问题的一部分。

    在你调用的 Config.js 下:

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

    然后在 index.js 下你调用了:

    import {Router, BrowserRouter } from 'react-router';
    

    如果这不能解决问题,您能否创建一个代码框以便我查看? https://codesandbox.io/

    以下是有关您尝试使用的两个 npm 包的更多信息: https://www.npmjs.com/package/react-router
    https://www.npmjs.com/package/react-router-dom
    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 2017-02-11
      • 1970-01-01
      • 2017-07-18
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2016-09-02
      相关资源
      最近更新 更多