我们可以使用 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:
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 submission307— 用于临时重定向¥
307— for temporary redirects308— 用于永久重定向¥
308— for permanent redirects
redirect(...)会抛出异常,就像error(...)一样,这意味着重定向后的代码不会运行。
<p>home</p>