[英]How do I redirect using a relative URL?
I am trying to redirect a Sign Out button from a dropdown on my page.我正在尝试从我的页面上的下拉列表中重定向注销按钮。 When I click on the sign out as of right now it will go to localhost:3000/signout.
当我现在单击注销时,它将 go 到 localhost:3000/signout。 I have tried:
我努力了:
export const SIGNOUT="redirect=www.google.com";
and it will simply replace the URL as localhost:3000/redirect=www.google.com.它将简单地将 URL 替换为 localhost:3000/redirect=www.google.com。
I have tried:我努力了:
<Route exact path={SIGNOUT}>
<Redirect to={www.google.com}/>
</Route>
export const SIGNOUT="www.google.com";
This will redirect to google.com upon loading and won't even let me load my own webpage:这将在加载时重定向到 google.com,甚至不会让我加载自己的网页:
export const SIGNOUT= window.location.replace("http://www.google.com");
urlLists.js
urlLists.js
export const SIGNOUT= "www.google.com";
App.js
应用程序.js
import {SIGNOUT} from "./utils/urlLists";
class App extends Component {
render() {
const {location} = this.props
return (
<Switch>
<Route exact path={SIGNOUT}>
<Redirect to={HOME}/>
</Route>
</Switch>
);
}
}
export default withRouter(App);
I expect the results of this to redirect to Google upon clicking on the Sign Out dropdown option.我希望单击“退出”下拉选项后,结果会重定向到 Google。
The actual result is either a redirection to:实际结果是重定向到:
localhost:3000/www.google.com
or the Google page is loaded and my localhost:3000 does not.或者 Google 页面已加载,而我的 localhost:3000 没有。
The idea is to redirect inside your Home
component.这个想法是在您的
Home
组件内重定向。 Take a look at this sample implementation.看看这个示例实现。
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function Home() {
return (
<div className="App">
<h1>Hello World</h1>
<button
name="sign-out"
onClick={() => {
// sign user out here
window.location.assign("www.google.com");
}}
>
Sign Out
</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Home />, rootElement);
The Redirect
component is nice for redirecting to an internal route . Redirect
组件非常适合重定向到内部路由。 However, if you're kicking the user out of your app you should use something else like window.location.assign()
.但是,如果您将用户踢出您的应用程序,您应该使用其他东西,例如
window.location.assign()
。
You can try this:你可以试试这个:
import React from 'react'
import { Redirect } from 'react-router-dom'
const ProtectedComponent = () => {
if (authFails)
return <Redirect to='redirect=www.google.com' />
}
return <div> My Protected Component </div>
}
Whatever included in the "" is the thing added to the domain. “”中包含的任何内容都是添加到域中的内容。 For example export const SIGNOUT= window.location.replace("google");
例如 export const SIGNOUT= window.location.replace("google"); will become localhost:3000/google.
将成为 localhost:3000/google。
If you want to use "www.google.com", try to import it as a thing of its own at the beginning of the page, like "import Google from "www.google.com", then use the {Google} element.如果您想使用“www.google.com”,请尝试在页面开头将其作为自己的东西导入,例如“从“www.google.com”导入 Google,然后使用 {Google} 元素.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.