我使用React-Router 1.0.0-rc3和Redux-Router 1.0.0-beta3。
使用React-Router时,您可以使用destroy
和$scope.$on('$destroy', function(){
// do cleanup
});
来设置应用的基本URL,以便您可以轻松编写在子目录中运行的应用。 Example:
而不是:
useBasename
您可以使用createHistory
:
import { createHistory } from 'history';
let base = "/app_name/"
<Router history={browserHistory}>
<Route path={base} component={App}></Route>
</Router>
<Link path={base + "some_path"}>some_path</Link>
但是,在Redux-Router中,您需要将useBasename
而不是import { createHistory, useBasename } from 'history';
const browserHistory = useBasename(createHistory)({
basename: "/app_name"
});
<Router history={browserHistory}>
<Route path="/" component={App}></Route>
</Router>
<Link path="/some_path">some_path</Link>
传递给reducer:
createHistory
在这种情况下我们如何使用history
?
答案 0 :(得分:12)
对于react-router v2或v3并使用react-router-redux v4而不是redux-router,历史对象的设置如下所示:
import { createHistory } from 'history'
import { useRouterHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
const browserHistory = useRouterHistory(createHistory)({
basename: '<yourBaseUrl>'
})
const history = syncHistoryWithStore(browserHistory, store)
当没有额外的基本URL时,其余的设置就像往常一样。
答案 1 :(得分:2)
您可以创建一个包裹const store = compose(
applyMiddleware(m1, m2, m3),
reduxReactRouter({
routes,
createHistory
}),
devTools()
)(createStore)(reducer);
的函数:
useBasename
并将其传递给useBasename
:
const createHistoryWithBasename = (historyOptions) => {
return useBasename(createHistory)({
basename: '/app_name',
...historyOptions
})
}
答案 2 :(得分:1)
API经常更改,因此这对我有用(我正在使用2.0.3
版本的redux-simple-router
)。我已在单独的文件中定义了自定义历史记录:
import { useRouterHistory } from 'react-router'
import { createHistory, useBasename } from 'history'
import { baseUrl } from '../config'
const browserHistory = useRouterHistory(useBasename(createHistory))({
basename: "/appgen"
});
现在我需要初始化商店:
import { syncHistory, routeReducer } from 'redux-simple-router'
import browserHistory from '../misc/browserHistory'
const rootReducer = combineReducers({
// ...other reducers
routing: routeReducer
});
const reduxRouterMiddleware = syncHistory(browserHistory);
const finalCreateStore = compose(
// ...other middleware
applyMiddleware(reduxRouterMiddleware),
)(createStore);
const store = finalCreateStore(rootReducer, initialState);
最后,您需要将history
传递给Router
。
import browserHistory from './misc/browserHistory'
import routes from '../routes'
export default class Root extends Component {
static propTypes = {
history: PropTypes.object.isRequired
};
render() {
return (
<Router history={browserHistory}>
{routes}
</Router>
)
}
}
在我提出这个问题之前,我使用的是手动解决方案。我定义了自己的Link
组件和我自己的redux操作,它们负责预先添加基本URL。它可能对某人有用。
更新了Link
组件:
import React, { Component } from 'react'
import { Link as RouterLink } from 'react-router'
import { baseUrl } from '../config'
export default class Link extends Component {
render() {
return <RouterLink {...this.props} to={baseUrl + '/' + this.props.to} />
}
}
自定义动作创建者:
import { routeActions } from 'redux-simple-router'
import { baseUrl } from '../config'
export function goTo(path) {
return routeActions.push(baseUrl + '/' + path);
}
更新了根路径:
import { baseUrl } from './config'
export default (
<Route component={App} path={baseUrl}>
//...nested routes
</Route>
);
请注意,这些自定义工具仅支持推送更新的路径,而不支持位置描述符对象。
答案 3 :(得分:-1)
迭戈五世的答案变体对我有用(不知道我在做什么):
import { createHistory } from 'history'
import { useRouterHistory } from 'react-router'
...
const _browserHistory = useRouterHistory(createHistory)({
basename: '/yourbase'
})
const store = createStore(_browserHistory, api, window.__data)
const history = syncHistoryWithStore(_browserHistory, store)
我正在替换:
//const _browserHistory = useScroll(() => browserHistory)()