【问题标题】:React Router: Location "/" Not Recognized?反应路由器:位置“/”无法识别?
【发布时间】:2021-02-07 19:12:45
【问题描述】:

我已经让 React Router 正常工作了 。现在突然今天早上它停止工作了。我正在使用 React Router 5。

我创建了一个小型演示项目来演示异常情况。这是路由器:

import React from 'react'
import {Route, Router, Switch} from 'react-router';
import {Link} from 'react-router-dom'
import History from 'history';

const browserHistory = History.createBrowserHistory();
console.log(browserHistory.location);

export function App() {
    return (
        <Router history={browserHistory}>
            <Switch>
                <Route path="/" render={(routeProps) =>
                    <Link to={"/"}>Home</Link>
                }/>
                <Route render={(routeProps) =>
                    <Link to={"/"}>It's the same location ("/"). Why did I go to a different route?</Link>
                 }/>
            </Switch>
        </Router>
    )
}

点击“主页”,应该会重新显示主页...但它没有。

Here's a dropbox link to download the tiny demo app。 Meteor 是构建工具,所以要运行它:

  • meteor npm install
  • meteor run
  • 查看应用程序http://localhost:3000/

我错过了什么?

【问题讨论】:

    标签: reactjs meteor react-router


    【解决方案1】:

    好的,我修好了。

    代替:

    import {Route, Router, Switch} from 'react-router';
    import {Link} from 'react-router-dom'
    

    ...它必须是:

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

    现在的问题是,为什么它曾经以第一种方式起作用?我的应用多年来一直如此,直到昨天早上它都运行良好。

    如果有人可以在一两个月内发布该问题的答案,我会在此处将其标记为已接受的答案。

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 2019-11-16
      • 2018-05-31
      • 2012-07-15
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2014-01-30
      • 2019-03-18
      相关资源
      最近更新 更多