- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Sapper 中实现 next.js
中所做的事情名为 with-route-as-modal 的示例.
它的作用是,当单击链接时,新页面以模态显示而不是替换当前页面,并且 URL 会更新,以反射(reflect)当前模态页面。它在多个社交网络中实现,例如 instagram。
在 next.js
例如,它是通过使用动态 href 来完成的,如下所示:
<Link href={`/?postId=${id}`} as={`/post/${id}`}>
最佳答案
我设法以这种方式做到了:
<script>
import { prefetch } from '@sapper/app'
import { onMount, onDestroy } from 'svelte'
import Post from '../components/Post.svelte'
let props
onMount(() => {
window.onpopstate = function (event) {
if (document.location.pathname === '/about') {
props = null
} else {
const regex = /\/blog\/([\w-]+)/
if (regex.test(document.location.pathname)) {
handlePrefetch(document.location.pathname)
}
}
}
return () => {
window.onpopstate = null
}
})
function handleClick(event) {
event.preventDefault()
const clickedHref = event.currentTarget.href
if (clickedHref === location.href) return
const pathname = clickedHref.replace(location.origin, '').substring(1)
history.pushState(null, '', pathname)
handlePrefetch(pathname)
}
async function handlePrefetch(url) {
const res = await prefetch(url)
const { branch } = res
props = branch[1].props.post
}
function handleClose() {
history.pushState(null, '', 'about')
props = null
}
</script>
<svelte:head>
<title>About</title>
</svelte:head>
<h1>About this site</h1>
<p>This is the 'about' page. There's not much here.</p>
<a href="/blog/what-is-sapper" on:click="{handleClick}">lien ici</a>
{#if props}
<Post title="{props.title}" html="{props.html}"></Post>
<button on:click="{handleClose}"></button>
{/if}
我必须手动处理弹出状态事件(这样后退按钮仍然有效)。然后我用工兵
prefetch
函数并将生成的 Prop 作为本地 Prop 注入(inject)。然后我检查是否设置了任何 Prop ,然后根据它注入(inject)自定义 HTML。
<Post title={post.title} html={post.html} />
的工兵组件。
关于routing - 在 Sapper 中作为模态路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61185073/
我想在 Sapper 中实现一个简单的页面(路由)转换。例如,使用 Nuxt 很容易实现的东西。有谁知道如何使用 Sapper 实现这一点? 我已经使用 transition:fade 指令将我的页面
我过去在expressjs中使用过passportjs,目前我正试图将它与Sapper应用程序合并,但我无法弄清楚如何在我的 route 包含passport.authenticate(),因为它是一
我想为管理员呈现 500 错误的回溯来自服务器,所以: 在 server.js 中, session 由从 http_only cookie 中检索到的用户填充,类似于 {'username': 'a
我在 Svelte 中有一个可读存储,如下所示: const state = {}; export const channels = readable(state, set => {
我正在使用Svelte和 Sapper对于一个网络应用程序,我需要动态地进入下一页,即在发生某些事情之后(网络蓝牙连接) - 而不仅仅是从 元素点击。 对于链接,Sapper 拦截这些并执行客户端路由
我目前正在学习 Sapper 并将其与 GraphQL 服务集成。 为了从一个简单的地方开始,我制作了一个常见问题解答页面,其中包含一个简单的问题/答案列表以及一个创建表单。 import gr
问题是我有两条路线/istifta 和 /istifta/edit /istifta 路由显示 istiftas(问题),而 /istifta/edit 路由由于相同的 _layout 而在同一页面上
我正在尝试在 Sapper 中实现 next.js 中所做的事情名为 with-route-as-modal 的示例. 它的作用是,当单击链接时,新页面以模态显示而不是替换当前页面,并且 URL 会更
在 Svelte 你可以通过 props to the content in a slot . {message} 当 Sapper 使用布局渲染路由时,路由内
我有一个 节点正在运行的开发服务器 工兵 在 http://localhost:3000 ,我想要所有 /api/请求代理另一个用 python 编写的本地开发服务器 http://localhost
假设我有一个 /foo路线。但有时人们会打/foo带有语言参数:/fr/foo .有时他们可能会用一种语言和一个国家来打击它:/ca/fr/foo 所以我需要一个路由表 [country]/[lang
我有一条动态路线: routes/artwork/[slug].svelte。 一切都与 npm run dev 配合得很好。 但是当我 npm run export 时,那些动态 slug 路由丢失
我有一条动态路线: routes/artwork/[slug].svelte。 一切都与 npm run dev 配合得很好。 但是当我 npm run export 时,那些动态 slug 路由丢失
我试图弄清楚如何调试服务器端以对默认工兵模板进行一些修改,我按照说明从文档中调试服务器端,并且 ndb 正确打开,但出现的唯一文件加载在gui 是 webpack.config.js 文件: scre
目前,我正在做一个共享 Menu 的项目。除两个页面外的所有页面的组件。我在根目录中添加了菜单组件 _layout.svelte文件。现在因为两个页面不需要Menu组件是嵌套路由。 Menu由于它们是
我正在使用 Sapper与 Svelte创建一个静态网站。 因为我的网站是静态的,所以我不会为服务器烦恼,而是会使用 sapper export命令生成我可以在 GitLab 页面上托管的静态文件。
我正在使用 axios 在 preload 内外发送请求。但我不明白出于某种原因我的后端提示必须提供 JWT。我在另一个前端使用相同的后端(Svelte 仅与 Axios 一起使用)并且没有提示。但是
不断从客户端收到此警告,但不完全确定是什么导致了它。任何正确方向的指导都会非常有帮助! • client The 'this' keyword is equivalent to 'undefined'
我正在玩一个新的 Sapper 模板 ("sveltejs/sapper-template#webpack") 并想在 src/server.js 中设置一个断点使用网络 Storm 。设置断点很容易
我正在将 Firebase 导入到我的 Sapper 应用程序中,我不希望在服务器上评估导入。如何确保导入仅在客户端? 我正在使用 Sapper 运行 sapper export生成静态文件。我试过了
我是一名优秀的程序员,十分优秀!