javascript - 如果找不到路由,则以编程方式导航到服务器页面

标签 javascript reactjs react-router

我有代码在超时后导航到某个网址:

componentDidMount: function() {
    var self = this;
    setTimeout(function() { self.context.history.pushState(null, '/somePage'); }, 5000);
},

但我希望从服务器加载此页面,而不是作为 SPA 代码中的组件。 所以当我这样做时,我只需:

Warning: Location "/somePage" did not match any routes

什么也没发生。 如何以正确的方式去做呢? 谢谢。

最佳答案

你可以使用:

window.location = "/somePage";

所以:

componentDidMount: function() {
  var self = this;
  setTimeout(function() { window.location="/somePage"; }, 5000);
},

关于javascript - 如果找不到路由,则以编程方式导航到服务器页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36681697/

相关文章:

Javascript 数组、图像链接、链接

javascript - Subject.subscribe 在构造函数之外不工作

javascript - React 中最令人困惑的不变违规

arrays - React Native 对象子映射在父映射内

javascript - React Router 如何从路径字符串中推断强类型参数?

javascript - 如何导航到 React Router Dom V6 中的嵌套路由

javascript - 将命令的响应传输到 Node

javascript - jQuery 计时问题 (ajax)

javascript - 如何防止嵌入的字体在加载后滚动

javascript - 如何为react-router Link的onClick函数执行设置超时?