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.
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.
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.
createClassComponent,
function handlers(...handlers: EventListener[]): EventListener
Function to mimic the multiple listeners available in svelte 4
run,
function self(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void
Substitute for the self
event modifier
self,
function stopImmediatePropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void
Substitute for the stopImmediatePropagation
event modifier
stopImmediatePropagation,
function stopPropagation(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void
Substitute for the stopPropagation
event modifier
stopPropagation,
function trusted(fn: (event: Event, ...args: Array<unknown>) => void): (event: Event, ...args: unknown[]) => void
Substitute for the trusted
event modifier
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>>
>;
};