简体   繁体   English

React中的绝对路径重定向

[英]Absolute path redirect in React

I'm trying to build a bit.ly clone and I would like to redirect to an exact URL. 我正在尝试建立一个bit.ly克隆,我想重定向到一个确切的URL。

Unfortunately if I use window.location.replace(str) in some cases it believes I would like to redirect to a relative URL. 不幸的是,如果我在某些情况下使用window.location.replace(str),它认为我想重定向到相对URL。 For example str = "google.com". 例如str =“ google.com”。

I'm wondering if there is a way to force an absolute path redirect that I am missing 我想知道是否有一种方法可以强制我丢失的绝对路径重定向

Use a URL with a schema. 使用带有架构的URL。 google.com is a relative URL, like image.jpg . google.com是相对网址,例如image.jpg

 location.replace('google.com'); 

 location.replace('http://stackoverflow.com'); 

import {withRouter} from 'react-router-dom';

Try importing withRouter and wrap the component with it. 尝试使用withRouter导入并用它包装组件。

export default (withRouter(MyComponent))

Now, you can use 现在,您可以使用

this.props.history.replace('// your link');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM