Skip to main content
基本 Svelte
介绍
反应性
属性
逻辑
事件
绑定
类和样式
动作
转换
高级 Svelte
高级反应性
重用内容
运动
高级绑定
高级转换
上下文 API
特殊元素
<script module>
后续步骤
基本 SvelteKit
介绍
路由
加载数据
标题和 cookie
共享模块
表单
API 路由
$app/state
错误和重定向
高级 SvelteKit
钩子
页面选项
链接选项
高级路由
高级加载
环境变量
结论

我们可以使用 redirect 机制从一个页面重定向到另一个页面。

¥We can use the redirect mechanism to redirect from one page to another.

src/routes/a/+page.server.js 中创建一个新的 load 函数:

¥Create a new load function in src/routes/a/+page.server.js:

src/routes/a/+page.server
import { redirect } from '@sveltejs/kit';

export function load() {
	redirect(307, '/b');
}

导航到 /a 现在会直接带我们到 /b

¥Navigating to /a will now take us straight to /b.

你可以在 load 函数、表单操作、API 路由和 handle 钩子中使用 redirect(...),我们将在后面的章节中讨论。

¥You can redirect(...) inside load functions, form actions, API routes and the handle hook, which we’ll discuss in a later chapter.

你将使用的最常见的状态代码:

¥The most common status codes you’ll use:

  • 303 — 用于成功提交后的表单操作

    ¥303 — for form actions, following a successful submission

  • 307 — 用于临时重定向

    ¥307 — for temporary redirects

  • 308 — 用于永久重定向

    ¥308 — for permanent redirects

上一页 下一页
1
2
<p>home</p>