原生history库 createBrowser方法中的history.push会导致file not found

发布于 2022-09-30 23:08:35 字数 1121 浏览 40 评论 0

我在学习过程中引用原生history库到我的html文件里面,然后在使用的时候createHashHistory()方法创建的history对象是完全没问题的,但是createBrowserHistory()创建的对象进行push的时候,地址栏被清空了,就剩下一个我最后push进去的地址/test,导致file not found报错.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>History</title>
  </head>
  <body>  
 <button onclick="window.push('/test')">click me to push test </button>
    <script type="module">
      import { createBrowserHistory } from "https://unpkg.com/history/history.production.min.js"
      let history = createBrowserHistory()

      window.push = function push(path) {
        history.push(path)
        return false
      }

      history.listen(({ action, location }) => {
          console.log( `The current URL is ${location.pathname}${location.search}${location.hash}`);
          console.log(`The last navigation action was ${action}`); 
      });
    </script>
</body>
</html>

跪求路过的大佬给答疑.肥肠感谢.肥肠肥肠感谢~~~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2022-10-07 23:08:35

你是不是在资源管理器/访达里直接双击打开文件、以 file 形式浏览的网页?

你需要放到一个 WebServer 下、以 http 的形式访问才可以使用 history。

如果用的是 VSCode 作为 IDE 的话可以装个 LiveServer 插件,本地启动一个 WebServer。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文