本文讲解的是引用redux-saga后的路由跳转问题。提供总共两种方式javascript
1.首先安装history和react-router-reduxjava
yarn add history react-router-reduxreact
2.在store里面引用redux
import { createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducer from './reducers';
import sagas from './sagas'
import { routerMiddleware } from 'react-router-redux';
const sagaMiddleware = createSagaMiddleware();
const createHistory = require('history').createHashHistory;
const history = createHistory(); //初始化history
const routerWare = routerMiddleware(history);
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose;
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware, routerWare));
const store = createStore(
reducer,
enhancer
)
sagaMiddleware.run(sagas)
export default store
复制代码
3.在saga中应用react-router
import { push } from 'react-router-redux';
function* login() {
if (...) {// 登陆成功,路由跳转
yield put(push('/'))
}
}
复制代码
1.一样须要安装historyapp
yarn add historyide
2.封装工具类history.js工具
import createHistory from 'history/createHashHistory'; //必定要注意分清楚createHashHistory和createBrowserHistory两种路由方式的区别
export const history = createHistory()
复制代码
3.主入口中引用ui
import { history } from '@/utils/history';
<HashRouter history={ history }> <Provider store={ store }> <App /> </Provider> </HashRouter>
复制代码
4.sagaspa
import { history } from '@/utils/history';
function* login() {
if (...) {// 登陆成功,路由跳转
yield put(history.replace('/'))
}
}
复制代码