【发布时间】: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