Skip to main content
Svelte基础
介绍
响应
属性Props
逻辑表达式
事件
绑定
Classes和样式
动作Actions
转场
Svelte高阶
响应式进阶
内容复用
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion

The event object passed into handle is the same object — an instance of a RequestEvent — that is passed into API routes in +server.js files, form actions in +page.server.js files, and load functions in +page.server.js and +layout.server.js.

It contains a number of useful properties and methods, some of which we’ve already encountered:

  • cookies — the cookies API
  • fetch — the standard Fetch API, with additional powers
  • getClientAddress() — a function to get the client’s IP address
  • isDataRequesttrue if the browser is requesting data for a page during client-side navigation, false if a page/route is being requested directly
  • locals — a place to put arbitrary data
  • params — the route parameters
  • request — the Request object
  • route — an object with an id property representing the route that was matched
  • setHeaders(...) — a function for setting HTTP headers on the response
  • url — a URL object representing the current request

A useful pattern is to add some data to event.locals in handle so that it can be read in subsequent load functions:

src/hooks.server
export async function handle({ event, resolve }) {
	event.locals.answer = 42;
	return await resolve(event);
}
src/routes/+page.server
export function load(event) {
	return {
		message: `the answer is ${event.locals.answer}`
	};
}

Edit this page on GitHub

previous next
<script>
let { data } = $props();
</script>

<h1>{data.message}</h1>
booting webcontainer