1.从props取出并传递history

取 const { history } = this.props

用 <button onClick={ () => history.push('/') }>go back home</button>

2.withRouter, Link

withRouter:

import { withRouter, Link } from 'dva/router'

<button onClick={ () => history.push('/') }>go back home</button>

export default withRouter(Counter);

Link:

import { withRouter, Link } from 'dva/router'; // 引入组件 

<Link to='/'>home page</Link>   使用

3.routerRedux

import { routerRedux } from 'dva/router';

effects: {
*asyncDecr({ payload }, { call, put }) {
yield call(delay, );
yield put({type: 'decrement' });
yield put( routerRedux.push('/') ); // 路由跳转
}
},

使用query-string库可以将对象转化为url参数:

effects: {
*asyncDecr({ payload }, { call, put }) {
yield call(delay, );
yield put({type: 'decrement' });
// yield put( routerRedux.push('/') ); // 路由跳转
yield put( routerRedux.push({
pathname: '/',
search: queryString.stringify({
from: 'product',
to: 'home'
})
}) ); // 路由跳转
}
},

效果:

http://localhost:8000/?from=product&to=home

完整代码:

第一个是model文件products.js   第二个是routes下的UI文件ProductPage.js

import { delay } from 'dva/saga';
import { routerRedux } from 'dva/router';
import queryString from 'query-string'; export default {
namespace: 'products',
state: {
counter: ,
},
effects: {
*asyncDecr({ payload }, { call, put }) {
yield call(delay, );
yield put({type: 'decrement' });
// yield put( routerRedux.push('/') ); // 路由跳转
yield put( routerRedux.push({
pathname: '/',
search: queryString.stringify({
from: 'product',
to: 'home'
})
}) ); // 路由跳转
}
},
reducers: {
'increment'(state, action) {
return {
counter: state.counter +
}
},
'decrement'(state, action) {
return {
counter: state.counter -
}
}
}
}
import React, { Component } from 'react';
import { connect } from 'dva';
import propTypes from 'prop-types';
import { Button } from 'antd';
import styles from './ProductPage.css';
import { increment, asyncDecr } from '../actions'; class ProductPage extends Component {
constructor(props, context) {
console.log(props);
super();
}
render() {
const { products, dispatch, increment, asyncDecr } = this.props;
return (
<div className={styles.wrapper}>
<div className={styles.title}>结果 {products.counter}</div>
<div>
<p className={styles['p-wrapper']}>
<Button type="primary" onClick={()=>dispatch({type:'products/increment',payload:})}>incr</Button>&nbsp;&nbsp;
<Button type="primary" onClick={()=>dispatch({type:'products/asyncDecr',payload:})}>incr</Button>
</p>
<p className={styles['p-wrapper']}>
<Button type="primary" onClick={()=>increment()}>increment</Button>&nbsp;&nbsp;
<Button type="primary" onClick={()=>asyncDecr()}>asyncDecr</Button>
</p>
<Button type="primary">decr</Button>
</div>
</div>
);
}
} ProductPage.propTypes = {
counter: propTypes.object
}; const mapStateToProps = (state) => {
return {
products: state.products,
};
}; export default connect(mapStateToProps, { increment, asyncDecr })(ProductPage);

这里是最后一种路由跳转方式,可以轻松应对各种场景 https://www.tipsns.com/read/34.html

dva 路由跳转的更多相关文章

  1. 前端笔记之React(七)redux-saga&Dva&路由

    一.redux-saga解决异步 redux-thunk 和 redux-saga 使用redux它们是必选的,二选一,它们两个都可以很好的实现一些复杂情况下redux,本质都是为了解决异步actio ...

  2. vue路由跳转时判断用户是否登录功能

    通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以 ...

  3. Extjs6(四)——侧边栏导航根据路由跳转页面

    本文基于ext-6.0.0 之前做的时候这个侧边栏导航是通过tab切换来切换页面的,但是总感觉不太对劲,现在终于发现怎么通过路由跳转了,分享给大家,可能有些不完善的地方,望大家读后可以给些指点.欢迎留 ...

  4. 关于elementUi tab组件路由跳转卡死问题

    好久没来了,周五项目终于要上线了(*^▽^*),上线之前测出一个很恶心的bug真真是... 项目:Vue + elementUi   后台管理项目 问题描述:登录后首次通过侧边栏路由跳转到主页面有ta ...

  5. 10. vue axios 请求未完成时路由跳转报错问题

    axios 请求未完成时路由跳转报错问题 前两天项目基本功能算是完成了,在公司测试时遇到了遇到了一个问题,那就是在请求未完成时进行路由跳转时会报错,想了几种办法来解决,例如加loading,请求拦截, ...

  6. react-router(v4) 路由跳转后返回页面顶部问题

    遇到的问题 由A页面跳转到B页面,B页面停留在A页面的位置,没有返回到顶部. 问题分析 首先分析下出现此问题的原因: 在项目中使用的是 hashHistory,它是建立在 history 之上的,当路 ...

  7. 2种方式解决vue路由跳转未匹配相应路由避免出现空白页面或者指定404页面

    https://www.cnblogs.com/goloving/p/9254084.html https://www.cnblogs.com/goloving/p/9254084.html 1.路由 ...

  8. vue设置路由跳转参数,以及接收参数

    最近做Vue项目,遇到了一个路由跳转问题:首页要跳转到项目页指定的Tab选项卡项,一开始总是跳到默认项.解决方法如下: 在跳转链接处设置了路由跳转参数,如下: <router-link  :to ...

  9. Taro-ui TabBar组件使用路由跳转

    1. 安装taro-ui (此处使用cnpm) cnpm install taro-ui 2. 全局引入样式 app.scss sass :@import "~taro-ui/dist/st ...

随机推荐

  1. 在.Net项目中使用Redis作为缓存服务

    转自:http://www.cnblogs.com/hohoa/p/5771255.html 最近由于项目需要,在系统缓存服务部分上了redis,终于有机会在实际开发中玩一下,之前都是自己随便看看写写 ...

  2. PHP中__get()和__set()的用法实例详

    刚刚看到一个对我有用的文章,我就把它摘抄下来了.                                                                        php面 ...

  3. 让Git不再难学

    写在前面 在团队做过软件开发的,版本控制必是不可或缺的一项.目前,版本控制主要分为集中式版本控制系统和分布式版本控制系统 ,即大家熟知的SVN和Git.Git是当下最流行的分布式版本控制系统,故,今天 ...

  4. [LuoguP2900] [USACO08MAR]土地征用(Land Acquisition)

    土地征用 (Link) 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽.比 ...

  5. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  6. Java中的IO流(三)

    上一篇<Java中的IO流(二)>把学习Java的字符流以及转换流作了一下记录,从本篇开始将把IO流中对文件或文件夹操作的对象File类的学习进行一下记录. 一,File类的构造函数及字段 ...

  7. 02 看懂Oracle执行计划

    看懂Oracle执行计划   最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing… 一:什么是Oracle执行计划? 执行计划是一条查询语句在 ...

  8. 【LightOJ 1081】Square Queries(二维RMQ降维)

    Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...

  9. 本地打jar包到本地的Maven出库

    1.命令行输入 mvn install:install-file -DgroupId=jar包的groupId -DartifactId=jar包的artifactId -Dversion=jar包的 ...

  10. MySQL(mariadb)多实例应用与多实例主从复制

    MySQL多实例 mysql多实例,简单理解就是在一台服务器上,mysql服务开启多个不同的端口(如3306.3307,3308),运行多个服务进程.这些 mysql 服务进程通过不同的 socket ...