dva 路由跳转
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>
<Button type="primary" onClick={()=>dispatch({type:'products/asyncDecr',payload:})}>incr</Button>
</p>
<p className={styles['p-wrapper']}>
<Button type="primary" onClick={()=>increment()}>increment</Button>
<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 路由跳转的更多相关文章
- 前端笔记之React(七)redux-saga&Dva&路由
一.redux-saga解决异步 redux-thunk 和 redux-saga 使用redux它们是必选的,二选一,它们两个都可以很好的实现一些复杂情况下redux,本质都是为了解决异步actio ...
- vue路由跳转时判断用户是否登录功能
通过判断该用户是否登录过,如果没有登录则跳转到login登录路由,如果登录则正常跳转. 一丶首先在用户登录前后分别给出一个状态来标识此用户是否登录(建议用vuex): 简单用vuex表示一下,不会可以 ...
- Extjs6(四)——侧边栏导航根据路由跳转页面
本文基于ext-6.0.0 之前做的时候这个侧边栏导航是通过tab切换来切换页面的,但是总感觉不太对劲,现在终于发现怎么通过路由跳转了,分享给大家,可能有些不完善的地方,望大家读后可以给些指点.欢迎留 ...
- 关于elementUi tab组件路由跳转卡死问题
好久没来了,周五项目终于要上线了(*^▽^*),上线之前测出一个很恶心的bug真真是... 项目:Vue + elementUi 后台管理项目 问题描述:登录后首次通过侧边栏路由跳转到主页面有ta ...
- 10. vue axios 请求未完成时路由跳转报错问题
axios 请求未完成时路由跳转报错问题 前两天项目基本功能算是完成了,在公司测试时遇到了遇到了一个问题,那就是在请求未完成时进行路由跳转时会报错,想了几种办法来解决,例如加loading,请求拦截, ...
- react-router(v4) 路由跳转后返回页面顶部问题
遇到的问题 由A页面跳转到B页面,B页面停留在A页面的位置,没有返回到顶部. 问题分析 首先分析下出现此问题的原因: 在项目中使用的是 hashHistory,它是建立在 history 之上的,当路 ...
- 2种方式解决vue路由跳转未匹配相应路由避免出现空白页面或者指定404页面
https://www.cnblogs.com/goloving/p/9254084.html https://www.cnblogs.com/goloving/p/9254084.html 1.路由 ...
- vue设置路由跳转参数,以及接收参数
最近做Vue项目,遇到了一个路由跳转问题:首页要跳转到项目页指定的Tab选项卡项,一开始总是跳到默认项.解决方法如下: 在跳转链接处设置了路由跳转参数,如下: <router-link :to ...
- Taro-ui TabBar组件使用路由跳转
1. 安装taro-ui (此处使用cnpm) cnpm install taro-ui 2. 全局引入样式 app.scss sass :@import "~taro-ui/dist/st ...
随机推荐
- geos学习笔记:安装和使用
1.首先在https://trac.osgeo.org/geos下载geos-3.6.2.tar.bz2 解压后 cd geos- ./configue //或选择安装的目录./configure - ...
- PHP中__get()和__set()的用法实例详
刚刚看到一个对我有用的文章,我就把它摘抄下来了. php面 ...
- 10、SpringBoot-CRUD登陆拦截
1.前端页面的设置 index.html <input type="text" class="form-control" name="usern ...
- Css3 实现关键帧动画
<div class="person"> </div> <script> var str1 = "@keyframes move{&q ...
- 【luogu P3366 最小生成树】 题解 Prim
include include include include using namespace std; const int maxn = 505000; int n, m, dis[maxn], v ...
- Java中int和String的转换问题
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- Android 复制 粘贴 剪贴板的使用 ClipboardManager
Copy and Paste 版本:Android 4.0 r1 快速查看 用于复制粘贴数据的基于剪贴板的框架. 同时支持简单和复杂的数据,包括文本串.复杂的数据结构.文本和二进制流数据.程序 as ...
- window 下创建软链接 mklink
软链接是一种文件共享方式. 命令:mklink /d "C:\d" "C:\e" 有哪些坑: 1.此命名必须以管理员方式在cmd运行 2.文件必须不存在..通过 ...
- 安装MySQL8.0.13
引用于:CrazyDemo,博客地址:http://www.cnblogs.com/CrazyDemo 下载地址: https://www.mysql.com/downloads/ 现在最下边的社区版 ...
- python类的使用(类定义,构造器,类属性,方法)
注:这里只描述使用方法,具体类的概念长篇大论就不要为难我这个懒人了. 总之一句话编程语言只是一个工具,会用就行,好用就行.打破砂锅问到底,我觉得有必要研究一下C,汇编,电子链路等. class clt ...