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. jquery分页例子

    先看效果图: 实现原理很简单,使用了jquery.pagination这个插件,每当点击页码时异步去服务器去取该页的数据,简单介绍如下: 一.数据库表结构:很简单  就四个字段 分别是News_id  ...

  2. #20145238荆玉茗《网络对抗》-逆向及Bof进阶实践

    20145238荆玉茗<网络对抗>-逆向及Bof进阶实践 实践目的:注入shellcode 准备一段shellcode代码 Shellcode实际是一段代码(也可以是填充数据),是用来发送 ...

  3. 使用@AspectJ注解开发Spring AOP

    一.实体类: Role public class Role { private int id; private String roleName; private String note; @Overr ...

  4. HDU 2082 找单词 (普通型 数量有限 母函数)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2082 找单词 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  5. 【SQLSERVER学习笔记】分页存储过程+调用

    USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[存储过程名] @pageI ...

  6. win7系统安装VS2013后,连不上远程sqlserver数据库解决办法

    刚搬了地方,顺便把电脑重做了系统,把sql2012和vs2013装好,怎么弄也连不上远程的数据库了,用程序连IIS直接死掉,用ssms连也是直接失去响应,开始以为是网线端口被运营商封杀了,最后发现不是 ...

  7. 【Java】使用Atomic变量实现锁

    Atomic原子操作 在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类 Java从JDK1.5开始提供了java.uti ...

  8. Java之变量

    Java变量分为类变量.实例变量.局部变量: 类变量包括静态变量: 局部变量:就是本地变量,使用范围:方法,构造器(构造方法),块:销毁:程序执行完或退出立即销毁:局部变量没有默认值,声明的同时必须赋 ...

  9. Docker镜像浅谈

    先抛出几个我在学习过程中产生的几个问题. 1. 容器镜像是什么, 和装系统时的镜像有什么关系? 2. 容器镜像的作用是什么? 3. 不同版本的ubuntu镜像有什么区别, 比如说 ubuntu:18. ...

  10. JavaScript 中 this 的原理

    一.问题 学习 JavaScript 其中一个标志就是理解下面两种写法,会输出有不一样的结果. var obj = { foo: function () {} }; var foo = obj.foo ...