dva 路由跳转

摘要:
1.从props取出并传递history取const{history}=this.props用history.push('/')}˃gobackhome2.withRouter,LinkwithRouter:import{withRouter,Link}from'dva/router'history.

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, 1000);
      yield put({type: 'decrement'});
      yield put( routerRedux.push('/') ); //路由跳转
}
  },

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

effects: {
    *asyncDecr({ payload }, { call, put }) {
      yield call(delay, 1000);
      yield put({type: 'decrement'});
      //yield put( routerRedux.push('/') ); //路由跳转
      yieldput( 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: 1,
  },
  effects: {
    *asyncDecr({ payload }, { call, put }) {
      yield call(delay, 1000);
      yield put({type: 'decrement'});
      //yield put( routerRedux.push('/') ); //路由跳转
      yieldput( routerRedux.push({
        pathname: '/',
        search: queryString.stringify({
          from: 'product',
          to: 'home'})
      }) ); //路由跳转
}
  },
  reducers: {
    'increment'(state, action) {
      return{
        counter: state.counter + 1}
    },
    'decrement'(state, action) {
      return{
        counter: state.counter - 1}
    }
  }
}
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';

classProductPage 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:1})}>incr</Button>&nbsp;&nbsp;
            <Button type="primary" onClick={()=>dispatch({type:'products/asyncDecr',payload:1})}>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 路由跳转》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇各种贝壳产地MAC地址记录与重复检测系统下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue 权限管理怎么做

前言 在一个项目中,一些功能会涉及到重要的数据管理,为了确保数据的安全,我们会在项目中加入权限来限制每个用户的操作。作为前端,我们要做的是配合后端给到的权限数据,做页面上的各种各样的限制。 需求 因为这是一个工作上的业务需求,所以对于我来说主要有两个地方需要进行权限控制。 第一个是侧边菜单栏,需要控制显示与隐藏。 第二个就是页面内的各个按钮,弹窗等。 流程...

利用Windows Media实现IP组播

随着互联网的迅猛发展,网络应用大量增加,尤其视频数据的传输量庞大。通过优化带宽可满足数据传输量增长的需要,IP组播技术则是优化带宽的重要手段。目前基于因特网的视频组播应用很广泛,如实时视频会议系统、远程教学系统、远程演示系统和视频点播系统(VOD)等。下面就IP组播最新技术的实现和应用进行讨论。 IP组播的必备条件 IP组播的设备支持组播源和组播组成员及其...

Android学习之五:android一些基本控件

每一个GUI开发工具都会提供一些基本的控件,例如Label和Button 等,下面我们来看下Android的一些基本控件。 Label:就是只用来显示些文本信息,而且不需要编辑的控件,在Android中是使用TextView控件的。我们来看一下在xml文件下面怎么定义该控件,我们来看以下的xml代码: <TextView android:lay...

BGP 实验

一、环境准备 1. 软件:GNS3 2. 路由:c7200 二、实验操作 实验要求: 1、 掌握 BGP 的基本配置方法。 2、 掌握如何查看 BGP 的各种配置信息。 3、 掌握基于回环口的 BGP 的邻居关系建立的配置方法。 4、 理解需要使用回环口为目的。 5、 理解 BGP 同步功能的作用和配置。 6、 掌握使用指向 NULL0 接口的静态路由的...

Springcloud之Zuul网关入门

Spring Cloud实现微服务的架构基本成型:   使用Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现;而服务间通过Ribbon或Feign实现服务的消费以及均衡负载。   为了使得服务集群更为健壮,使用Hystrix的融断机制来避免在微服务架构中个别服务出现异常时引起的故障蔓延。 Zuul是Netflix...

WPF默认控件模板的获取和资源词典的使用

一、获取默认的控件模板WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1、创建一个Button 2、在界面上选择Button,右键->编辑模板->编辑副本 ,即可看到XAML中自动生成了原始的控件模板 3、可以在默认模板上修改...