问题
Is there a way to get React Router to open a link in new tab? I tried this and it did not work.
<Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link>
It's possible to fluff it by adding something like onClick="foo" to the Link like what I have above, but there would be a console error.
Thanks.
回答1:
I think Link component does not have the props for it.
You can have alternative way by create a tag and use the makeHref method of Navigation mixin to create your url
<a target='_blank' href={this.makeHref(routeConsts.CHECK_DOMAIN, {},
{ realm: userStore.getState().realms[0].name })}>
Share this link to your webmaster
</a>
回答2:
In current version of React Router, you can use:
<Link to="route" target="_blank" onClick={(event) => {event.preventDefault(); window.open(this.makeHref("route"));}} />
回答3:
We can use the following options:-
// first option is:-
<Link to="myRoute" params={myParams} target="_blank">
// second option is:-
var href = this.props.history.createHref('myRoute', myParams);
<a href={href} target="_blank">
//third option is:-
var href = '/myRoute/' + myParams.foo + '/' + myParams.bar;
<a href={href} target="_blank">
We can use either of three option to open in new tab by react routing.
回答4:
Starting with react_router 1.0, the props will be passed onto the anchor tag. You can directly use target="_blank". Discussed here: https://github.com/ReactTraining/react-router/issues/2188
回答5:
For external link simply use an achor in place of Link:
<a rel="noopener noreferrer" href="http://url.com" target="_blank">Link Here</a>
回答6:
You can use "{}" for the target, then jsx will not cry
<Link target={"_blank"} to="your-link">Your Link</Link>
回答7:
The simples way is to use 'to' property:
<Link to="chart" target="_blank" to="http://link2external.page.com" >Test</Link>
回答8:
this works fine for me
<Link to={`link`} target="_blank">View</Link>
回答9:
In my case, I am using another function.
Function
function openTab() {
window.open('https://play.google.com/store/apps/details?id=com.drishya');
}
<Link onClick={openTab}></Link>
回答10:
target="_blank" is enough to open in a new tab, when you are using react-router
eg:
<Link to={/admin/posts/error-post-list/${this.props.errorDate}}
target="_blank"> View Details </Link>
来源:https://stackoverflow.com/questions/30202755/react-router-open-link-in-new-tab