代码之家  ›  专栏  ›  技术社区  ›  VikR

反应路由器:位置“/”未识别?

  •  0
  • VikR  · 技术社区  · 5 年前

    我已经有好几年没工作了 .今天早上它突然停止工作了。我用的是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/

    我错过了什么?

    0 回复  |  直到 5 年前
        1
  •  0
  •   VikR    5 年前

    好的,我修好了。

    而不是:

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

    ...它必须是:

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

    现在的问题是,为什么它会以第一种方式工作?我的应用程序已经有这么多年了,一直到昨天早上都运行得很好。

    如果有人能在一两个月内给出答案,我会在这里把它标记为被接受的答案。