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 ...
随机推荐
- BZOJ3143:[HNOI2013]游走(高斯消元)
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- 关于PHP数组你应该知道的事情
(1).PHP数组的遍历顺序 先举个栗子: <?php $arr['a'] = '123'; $arr['b'] = '456'; $arr['c'] = '789'; foreach($a a ...
- win7 xampp php7 yii2 配置sqlserver
第一步: https://www.microsoft.com/en-us/download/details.aspx?id=20098 下载 如下图 php7 版本 放到 xampp\php ...
- 课时46.label标签(掌握)
我们点击QQ注册页面,发现了一个问题,当我们点击密码两个字的时候,输入框聚焦了,而点击确认密码的时候,输入框也聚焦了,而我们上节课做的页面,这么点击,并不聚焦 1.默认情况下文字和输入框是没有关联关系 ...
- 给requests模块添加请求头列表和代理ip列表
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,符合了Python语言的思想,通俗的说去繁存 ...
- Spring知识点小结(四)
一.JdbcTemplate(jdbc模版--抽取的工具) web阶段DBUtils: QueryRunner runner = new QueryRunner(dataSource); ...
- js内部事件机制--单线程原理
原文地址:https://www.xingkongbj.com/blog/js/event-loop.html http://www.haorooms.com/post/js_xiancheng ht ...
- SQL语句中生成UUID方法
SQL语句中生成UUID方法为UUID() 生成带横线UUID: select UUID() 形如:abaffaca-fd55-11e5-b3d0-d2 ...
- 浅谈vue,小程序,react基础绑定值
最近一直在用react开发项目,碰见的问题千千万,很多,但是都殊途同源,唯一区别大的就是没有像vue的双向绑定,也没有小程序的单向方便,比如: vue v-modal="msg" ...
- Canvas状态的保存与恢复
Canvas的API提供了save()和restore()的方法,用于保存及恢复当前canvas绘图环境的所有属性. save()与restore()方法可以嵌套调用 save()方法将当前绘图环境压 ...