导航API

这些函数会触发浏览器历史会话中的状态更新,从而允许您以编程方式在页面之间进行转换。


goToPage

将新条目添加到浏览器的历史记录堆栈中。

typescript
function goToPage(
    pathname: string,
    pageData: Record<string, unknown> = {},
    title: string = document.title
): Promise<void>

示例

javascript
import { goToPage } from '@beforesemicolon/router'

await goToPage(
    '/users/42',
    { role: 'admin', scrollPosition: 0 },
    'User Profile'
)

replacePage

更新历史堆栈中的当前条目,而不是创建新条目。这对于重定向来说是理想的选择,因此单击浏览器的后退按钮不会使用户陷入重定向循环。

typescript
function replacePage(
    pathname: string,
    pageData: Record<string, unknown> = {},
    title: string = document.title
): Promise<void>

示例

javascript
import { replacePage } from '@beforesemicolon/router'

if (!isAuthenticated) {
    await replacePage('/login', { from: '/dashboard' }, 'Please Login')
}

previousPage

导航回浏览器会话中的上一个条目。与单击浏览器的后退按钮相同。

typescript
function previousPage(): void

nextPage

向前导航到浏览器会话中的下一个条目。与单击浏览器的前进按钮相同。

typescript
function nextPage(): void
编辑此文档