【问题标题】:React-router 1.0.2 is not working after configure with History module 1.13.1使用 History 模块 1.13.1 配置后,React-router 1.0.2 无法正常工作
【发布时间】:2015-12-14 13:42:28
【问题描述】:

在使用示例应用程序 here 配置 history 模块后,我遇到了这个问题。

我尝试在 react-router 文档Navigating Outside of Components 中遵循这种模式,并在

创建history.js

import createBrowserHistory from 'history/lib/createBrowserHistory';
export default createBrowserHistory();

router配置

import React from 'react';
import {render} from 'react-dom';
import { Router, Route, Link, IndexRoute } from 'react-router';
import createHistory from 'history/lib/createBrowserHistory';

import history from './config/history';
import routes from './config/routes';

let el = document.getElementById('login');
render(<Router history={history}>{routes}</Router>, el);

尝试使用here

import history from '../config/history';

我正在使用webpack,您可以使用npm run webpack 构建它,然后使用python -m SimpleHTTPServer 8080 构建服务器文件。 之后,当我想去http://localhost:8080/public/login.html#/setup时,它给出了以下错误。

Warning: Location "/public/login.html#/setup?_k=rnwzln" did not match any routes

如果我删除了Login.js 中的history={history},它会完美运行

你能帮我解决这个问题吗?是不是 react-router 的 bug?

P.S.:我已经查看了这个 stackoverflow question,但它并没有解决我的问题。

【问题讨论】:

    标签: react-router


    【解决方案1】:

    您正在使用 browserHistory 并期望哈希 url 匹配。

    https://github.com/rackt/react-router/blob/1.0.x/docs/guides/basics/Histories.md

    【讨论】:

    • +1,@rpflo 感谢您的帮助!情况就是这样。我只是想举一个例子,这不是我的用例。正如上面文档中提到的,react-router 默认使用哈希历史,并且可以通过this.props.history 从子组件访问它。因此更改路线可以通过this.props.history.pushState(null, '/new');
    猜你喜欢
    • 1970-01-01
    • 2022-12-04
    • 2018-07-30
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多