Skip to main content

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 函数,用于模拟没有 Svelte 4 处理程序的 on:click 行为。

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 编辑此页面

上一页 下一页