【问题标题】:Unexpected browserHistory behaviour (Meteor + React + React Router)意外的浏览器历史行为(Meteor + React + React Router)
【发布时间】:2016-06-30 06:17:35
【问题描述】:

(PS: 我用的是 Meteor + React + React Router。应用程序结构不是传统的,我在做一个 package-esq 应用程序,一个例子是https://github.com/TelescopeJS/Telescope。我正在尝试用反应路由器,事情进展不顺利。)

浏览器历史记录有问题。导航刷新页面。通过浏览器按钮来回切换会刷新页面。

此示例以及所有代码都在这里 - https://github.com/dbx834/sandbox

React-Router 具体代码如下,

在具有全局导出的核心包中,允许注册路由和组件

...
// ------------------------------------- Components -------------------------------- //

Sandbox.components = {};

Sandbox.registerComponent = (name, component) => {
  Sandbox.components[name] = component;
};

Sandbox.getComponent = (name) => {
  return Sandbox.components[name];
};

// ------------------------------------- Routes -------------------------------- //

Sandbox.routes = {};
Sandbox.routes.routes = [];

Sandbox.routes = {
  routes: [],
  add(routeOrRouteArray) {
    const addedRoutes = Array.isArray(routeOrRouteArray) ? routeOrRouteArray : [routeOrRouteArray];
    this.routes = this.routes.concat(addedRoutes);
  },
};
...

在各种实现(领域特定逻辑、UI 等)中,注册组件和路由

...
import TodoApp from './components/TodoApp.jsx';

Sandbox.registerComponent('TodoApp', TodoApp);

Sandbox.routes.add([
  { name: 'todoAppRoute', path: 'todo-app', component: Sandbox.components.TodoApp },
]);
...

在主应用中

import React from 'react';
import { render } from 'react-dom';
import { Meteor } from 'meteor/meteor';
import { Router, browserHistory } from 'react-router';

import App from './components/App.jsx';
import Homepage from './components/Homepage.jsx';

Sandbox.registerComponent('App', App);
Sandbox.registerComponent('Homepage', Homepage);

Meteor.startup(() => {

  const AppRoutes = {
    path: '/',
    component: Sandbox.components.App,
    indexRoute: { name: 'home', component: Sandbox.components.Homepage },
    childRoutes: Sandbox.routes.routes,
  };

  console.log(AppRoutes);

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

怎么了?

【问题讨论】:

    标签: meteor react-router


    【解决方案1】:

    我卸载了所有 npm 包、meteor 包,更新了所有内容,重新安装了最新的包,清理了所有以前的构建,现在一切正常!

    某处发生了一些奇怪的事情。

    如果有人发现自己处于类似情况,你可以试试这个。

    最好的

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2017-08-17
      • 2017-03-17
      • 2018-12-26
      • 2018-05-16
      • 2018-07-20
      • 2017-08-31
      • 2017-01-11
      • 2019-12-15
      相关资源
      最近更新 更多