【问题标题】:Sapper back button not working after history pushstate function历史推送状态功能后,Sapper后退按钮不起作用
【发布时间】:2019-08-03 17:26:12
【问题描述】:

我正在使用以下版本的 Sapper 和 Svelte:

"sapper": "^0.27.0",
"svelte": "^3.0.0"

使用history.pushState() 函数更新网址后,然后导航到其他页面。后退按钮没有返回到history.pushState()更改的url

【问题讨论】:

    标签: svelte sapper


    【解决方案1】:

    不要使用 history.pushState() — 这相当于在 Svelte 中使用自己的 element.appendChild(document.createElement('div'))。相反,使用 Sapper 的 goto 函数:https://sapper.svelte.dev/docs#goto_href_options

    【讨论】:

    • 对。我实际上是在尝试更改 input 字段更改的 url。
    • 我使用了goto(`/profile/edit?id=${profile_id}`, { replaceState: true })。我得到了想要的结果,但失去了表单字段的焦点。在goto()replaceState 之后,我的领域如何保持焦点?
    • 如何导入goto函数。
    • @YousufIqbalHashim 您可能需要保留对表单元素的引用并以编程方式设置焦点,或者如果它是一个新元素,则使用autofocus
    • @JoshuaUchennaAmaju import { goto } from '@sapper/app'
    【解决方案2】:

    我认为这是工兵https://github.com/sveltejs/sapper/issues/791 的问题

    提到的解决方法是将{id:0} 作为数据 传递给history.pushState()。例如

    history.pushState({id:0}, '', url)
    

    但要小心!,我们不知道这样做的后果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2023-01-22
      • 2011-12-31
      • 2015-10-11
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多