【问题标题】:React router open route in new window反应路由器在新窗口中打开路由
【发布时间】:2018-06-08 14:56:28
【问题描述】:

我试图在单击超链接时在新窗口中打开一条路线。 target=_blank 没有太大帮助,因为我试图在新窗口中打开,而不是在新标签中打开。 window.open 也会在新选项卡中打开。这有可能吗?

【问题讨论】:

  • 好的,window.open 工作正常,但我仍然不确定如何在其中打开一条新路线。

标签: reactjs react-router


【解决方案1】:

您可以使用它打开一个新窗口:

window.open('http://www.google.com', '_blank', 'toolbar=0,location=0,menubar=0');

如果您有不同的 url(开发环境、测试环境、产品环境),您可以使用它来获取基本 url:

window.location.origin

所以最终代码是:

window.open(window.location.origin + "/ROUTE_U_WANT", '_blank', 'toolbar=0,location=0,menubar=0');

【讨论】:

  • 感谢 Paolo,但这不起作用。它会打开一个新窗口,其中包含正确路由的 URL,但显示的内容与链接所在的原始页面相同
  • 嗨,如果这个例子是你想要的,请告诉我:class Hello extends React.Component { constructor(props){ super(props); this.onClick = this.onClick.bind(this); } onClick(){ window.open(window.location.origin + "/foreyez/vdk7guob/show", '_blank', 'toolbar=0,location=0,menubar=0'); } render() { return (<div> Hi <button onClick = {this.onClick}>Open new Window and new route</button> </div>); } } ReactDOM.render( <Hello/>, document.getElementById('container') ); 你确定你的应用路由有效吗?
  • 您好已经在 J​​SFiddle 中使用 React 测试了这段代码。可以粘贴到js内容里面here
  • 非常感谢!添加 # 如 window.open(window.location.origin + "#/newRoute",'_blank','toolbar=0,location=0,menubar=0');出于某种原因工作。
  • 我认为这是您实现路由的“方式”,它决定了您必须如何构建 URL :)
猜你喜欢
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2016-03-07
  • 2019-11-30
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多