跳至主要内容

svelte/legacy

此模块提供了在迁移期间使用的各种函数,因为某些功能无法一对一地替换为新功能。所有导入都被标记为已弃用,并且应随着时间的推移逐渐迁移。

import {
	function asClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(component: SvelteComponent<Props, Events, Slots> | Component<Props>): ComponentType<SvelteComponent<Props, Events, Slots> & Exports>

Takes the component function and returns a Svelte 4 compatible component constructor.

@deprecatedUse this only as a temporary solution to migrate your imperative component code to Svelte 5.
asClassComponent
,
function createBubbler(): (type: string) => (event: Event) => boolean

Function to create a bubble function that mimic the behavior of on:click without handler available in svelte 4.

@deprecatedUse this only as a temporary solution to migrate your automatically delegated events in Svelte 5.
createBubbler
,
function createClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(options: ComponentConstructorOptions<Props> & {
    component: ComponentType<SvelteComponent<Props, Events, Slots>> | Component<Props>;
}): SvelteComponent<Props, Events, Slots> & Exports

Takes the same options as a Svelte 4 component and the component function and returns a Svelte 4 compatible component.

@deprecatedUse this only as a temporary solution to migrate your imperative component code to Svelte 5.
createClassComponent
,
function handlers(...handlers: EventListener[]): EventListener

Function to mimic the multiple listeners available in svelte 4

@deprecated
handlers, function nonpassive(node: HTMLElement, [event, handler]: [event: string, handler: () => EventListener]): void

Substitute for the nonpassive event modifier, implemented as an action

@deprecated
nonpassive
,
function once(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the once event modifier

@deprecated
once
,
function passive(node: HTMLElement, [event, handler]: [event: string, handler: () => EventListener]): void

Substitute for the passive event modifier, implemented as an action

@deprecated
passive
,
function preventDefault(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the preventDefault event modifier

@deprecated
preventDefault
,
function run(fn: () => void | (() => void)): void

Runs the given function once immediately on the server, and works like $effect.pre on the client.

@deprecatedUse this only as a temporary solution to migrate your component code to Svelte 5.
run
,
function self(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the self event modifier

@deprecated
self
,
function stopImmediatePropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the stopImmediatePropagation event modifier

@deprecated
stopImmediatePropagation
,
function stopPropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the stopPropagation event modifier

@deprecated
stopPropagation
,
function trusted(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void

Substitute for the trusted event modifier

@deprecated
trusted
} from 'svelte/legacy';

asClassComponent

仅将此用作将您的命令式组件代码迁移到 Svelte 5 的临时解决方案。

获取组件函数并返回一个与 Svelte 4 兼容的组件构造函数。

function asClassComponent<
	Props extends Record<string, any>,
	Exports extends Record<string, any>,
	Events extends Record<string, any>,
	Slots extends Record<string, any>
>(
	component:
		| SvelteComponent<Props, Events, Slots>
		| Component<Props>
): ComponentType<
	SvelteComponent<Props, Events, Slots> & Exports
>;

createBubbler

仅将此用作将您在 Svelte 5 中自动委托的事件迁移的临时解决方案。

用于创建 bubble 函数的函数,该函数模拟 on:click 的行为,而 svelte 4 中没有可用的处理程序。

function createBubbler(): (
	type: string
) => (event: Event) => boolean;

createClassComponent

仅将此用作将您的命令式组件代码迁移到 Svelte 5 的临时解决方案。

获取与 Svelte 4 组件相同的选项和组件函数,并返回一个与 Svelte 4 兼容的组件。

function createClassComponent<
	Props extends Record<string, any>,
	Exports extends Record<string, any>,
	Events extends Record<string, any>,
	Slots extends Record<string, any>
>(
	options: ComponentConstructorOptions<Props> & {
		component:
			| ComponentType<SvelteComponent<Props, Events, Slots>>
			| Component<Props>;
	}
): SvelteComponent<Props, Events, Slots> & Exports;

handlers

用于模拟 svelte 4 中可用的多个侦听器的函数

function handlers(
	...handlers: EventListener[]
): EventListener;

nonpassive

替换 nonpassive 事件修饰符,实现为一个 action

function nonpassive(
	node: HTMLElement,
	[event, handler]: [
		event: string,
		handler: () => EventListener
	]
): void;

once

替换 once 事件修饰符

function once(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

passive

替换 passive 事件修饰符,实现为一个 action

function passive(
	node: HTMLElement,
	[event, handler]: [
		event: string,
		handler: () => EventListener
	]
): void;

preventDefault

替换 preventDefault 事件修饰符

function preventDefault(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

run

仅将此用作将您的组件代码迁移到 Svelte 5 的临时解决方案。

在服务器上立即运行一次给定的函数,并在客户端上像 $effect.pre 一样工作。

function run(fn: () => void | (() => void)): void;

self

替换 self 事件修饰符

function self(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

stopImmediatePropagation

替换 stopImmediatePropagation 事件修饰符

function stopImmediatePropagation(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

stopPropagation

替换 stopPropagation 事件修饰符

function stopPropagation(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

trusted

替换 trusted 事件修饰符

function trusted(
	fn: (event: Event, ...args: Array<unknown>) => void
): (event: Event, ...args: unknown[]) => void;

LegacyComponentType

在过渡期间支持将组件用作类和函数

type LegacyComponentType = {
	new (o: ComponentConstructorOptions): SvelteComponent;
	(
		...args: Parameters<Component<Record<string, any>>>
	): ReturnType<
		Component<Record<string, any>, Record<string, any>>
	>;
};

在 GitHub 上编辑此页面

上一页 下一页