以编程方式更改路由器路由参数

以编程方式更改路由器路由参数

本文介绍了以编程方式更改路由器路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单方便的方法导航到不同参数的相同路线?

Is there any simple and convenient way to navigate to same route with different params?

ex。我现在正在/ profile / idan,我希望将 idan 的参数更改为其他内容,立即不处理路线的静态部分。

for ex. im right now on /profile/idan and i wish to change the param of idan to something else, instantly without dealing with the static part of the route.

谢谢

推荐答案

如果你的问题是关于API的v3, react-router 库引入了路由器上下文。如果您在 contextTypes 中声明,则可以访问和方法。示例:

If your question is about v3 of the API, the react-router library introduces a router context. If you declare that in your contextTypes, you'll have access to a push and replace method. Example:

import React, { Component, PropTypes } from 'react';
import { PropTypes as RouterPropTypes } from 'react-router';

class MyComponent extends Component {
  // ...
  handleSomeEvent() {
    this.context.router.push('/some/url');
  }
}

MyComponent.contextTypes = {
  router: RouterPropTypes.routerShape
};

export default MyComponent;

这篇关于以编程方式更改路由器路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:31