导航阻止是一种防止导航发生的方式。这在用户尝试导航时通常会发生,当他们:
在这些情况下,应该向用户显示提示或自定义 UI 以确认他们想要离开。
导航阻止向整个底层历史 API 添加一个或多个"阻止器"层。如果存在任何阻止器,导航将通过以下方式之一暂停:
有 2 种使用导航阻止的方式:
假设我们想要在表单有未保存更改时阻止导航。我们可以使用 useBlocker hook 来实现:
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
useBlocker({
shouldBlockFn: () => {
if (!formIsDirty()) return false
const shouldLeave = confirm('您确定要离开吗?')
return !shouldLeave
},
})
// ...
}
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
useBlocker({
shouldBlockFn: () => {
if (!formIsDirty()) return false
const shouldLeave = confirm('您确定要离开吗?')
return !shouldLeave
},
})
// ...
}
shouldBlockFn 为您提供对 current 和 next 位置的类型安全访问:
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
// 始终阻止从 /foo 到 /bar/123?hello=world 的导航
const { proceed, reset, status } = useBlocker({
shouldBlockFn: ({ current, next }) => {
return (
current.routeId === '/foo' &&
next.fullPath === '/bar/$id' &&
next.params.id === 123 &&
next.search.hello === 'world'
)
},
withResolver: true,
})
// ...
}
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
// 始终阻止从 /foo 到 /bar/123?hello=world 的导航
const { proceed, reset, status } = useBlocker({
shouldBlockFn: ({ current, next }) => {
return (
current.routeId === '/foo' &&
next.fullPath === '/bar/$id' &&
next.params.id === 123 &&
next.search.hello === 'world'
)
},
withResolver: true,
})
// ...
}
您可以在 API 参考 中找到有关 useBlocker hook 的更多信息。
除了基于逻辑/hook 的阻止外,还可以使用 Block 组件来实现类似的结果:
import { Block } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
return (
<Block
shouldBlockFn={() => {
if (!formIsDirty()) return false
const shouldLeave = confirm('您确定要离开吗?')
return !shouldLeave
}}
/>
)
// OR
return (
<Block shouldBlockFn={() => !formIsDirty} withResolver>
{({ status, proceed, reset }) => <>{/* ... */}</>}
</Block>
)
}
import { Block } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
return (
<Block
shouldBlockFn={() => {
if (!formIsDirty()) return false
const shouldLeave = confirm('您确定要离开吗?')
return !shouldLeave
}}
/>
)
// OR
return (
<Block shouldBlockFn={() => !formIsDirty} withResolver>
{({ status, proceed, reset }) => <>{/* ... */}</>}
</Block>
)
}
在大多数情况下,在 hook 中使用 withResolver: false 并在 shouldBlockFn 函数中使用 window.confirm 就足够了,因为它会清楚地向用户显示导航正在被阻止,并根据他们的响应解决阻止。
但是,在某些情况下,您可能希望显示一个故意较少干扰且与您的应用程序设计更集成的自定义 UI。
注意: 如果 withResolver 为 true,shouldBlockFn 的返回值不会解决阻止。
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
const { proceed, reset, status } = useBlocker({
shouldBlockFn: () => formIsDirty(),
withResolver: true,
})
// ...
return (
<>
{/* ... */}
{status === 'blocked' && (
<div>
<p>您确定要离开吗?</p>
<button onClick={proceed}>是</button>
<button onClick={reset}>否</button>
</div>
)}
</>
}
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
const { proceed, reset, status } = useBlocker({
shouldBlockFn: () => formIsDirty(),
withResolver: true,
})
// ...
return (
<>
{/* ... */}
{status === 'blocked' && (
<div>
<p>您确定要离开吗?</p>
<button onClick={proceed}>是</button>
<button onClick={reset}>否</button>
</div>
)}
</>
}
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
useBlocker({
shouldBlockFn: () => {
if (!formIsDirty()) {
return false
}
const shouldBlock = new Promise<boolean>((resolve) => {
// 使用您选择的模态框管理器
modals.open({
title: '您确定要离开吗?',
children: (
<SaveBlocker
confirm={() => {
modals.closeAll()
resolve(false)
}}
reject={() => {
modals.closeAll()
resolve(true)
}}
/>
),
onClose: () => resolve(true),
})
})
return shouldBlock
},
})
// ...
}
import { useBlocker } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
useBlocker({
shouldBlockFn: () => {
if (!formIsDirty()) {
return false
}
const shouldBlock = new Promise<boolean>((resolve) => {
// 使用您选择的模态框管理器
modals.open({
title: '您确定要离开吗?',
children: (
<SaveBlocker
confirm={() => {
modals.closeAll()
resolve(false)
}}
reject={() => {
modals.closeAll()
resolve(true)
}}
/>
),
onClose: () => resolve(true),
})
})
return shouldBlock
},
})
// ...
}
与 hook 类似,Block 组件返回相同的状态和函数作为渲染属性:
import { Block } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
return (
<Block shouldBlockFn={() => formIsDirty()} withResolver>
{({ status, proceed, reset }) => (
<>
{/* ... */}
{status === 'blocked' && (
<div>
<p>您确定要离开吗?</p>
<button onClick={proceed}>是</button>
<button onClick={reset}>否</button>
</div>
)}
</>
)}
</Block>
)
}
import { Block } from '@tanstack/solid-router'
function MyComponent() {
const [formIsDirty, setFormIsDirty] = createSignal(false)
return (
<Block shouldBlockFn={() => formIsDirty()} withResolver>
{({ status, proceed, reset }) => (
<>
{/* ... */}
{status === 'blocked' && (
<div>
<p>您确定要离开吗?</p>
<button onClick={proceed}>是</button>
<button onClick={reset}>否</button>
</div>
)}
</>
)}
</Block>
)
}