svelte/reactivity
Svelte 提供各种内置组件(如 SvelteMap
、SvelteSet
和 SvelteURL
)的反应版本。这些可以从 svelte/reactivity
导入并像其原生对应项一样使用。
¥Svelte provides reactive versions of various built-ins like SvelteMap
, SvelteSet
and SvelteURL
. These can be imported from svelte/reactivity
and used just like their native counterparts.
<script>
import { SvelteURL } from 'svelte/reactivity';
const url = new SvelteURL('https://example.com/path');
</script>
<!-- changes to these... -->
<input bind:value={url.protocol} />
<input bind:value={url.hostname} />
<input bind:value={url.pathname} />
<hr />
<!-- will update `href` and vice versa -->
<input bind:value={url.href} />
import {
class MediaQuery
Creates a media query and provides a current
property that reflects whether or not it matches.
Use it carefully — during server-side rendering, there is no way to know what the correct value should be, potentially causing content to change upon hydration.
If you can use the media query in CSS to achieve the same effect, do that.
<script>
import { MediaQuery } from 'svelte/reactivity';
const large = new MediaQuery('min-width: 800px');
</script>
<h1>{large.current ? 'large screen' : 'small screen'}</h1>
MediaQuery,
class SvelteDate
SvelteDate,
class SvelteMap<K, V>
SvelteMap,
class SvelteSet<T>
SvelteSet,
class SvelteURL
SvelteURL,
class SvelteURLSearchParams
SvelteURLSearchParams,
function createSubscriber(start: (update: () => void) => (() => void) | void): () => void
Returns a subscribe
function that, if called in an effect (including expressions in the template),
calls its start
callback with an update
function. Whenever update
is called, the effect re-runs.
If start
returns a function, it will be called when the effect is destroyed.
If subscribe
is called in multiple effects, start
will only be called once as long as the effects
are active, and the returned teardown function will only be called when all effects are destroyed.
It’s best understood with an example. Here’s an implementation of MediaQuery
:
import { createSubscriber } from 'svelte/reactivity';
import { on } from 'svelte/events';
export class MediaQuery {
#query;
#subscribe;
constructor(query) {
this.#query = window.matchMedia(`(${query})`);
this.#subscribe = createSubscriber((update) => {
// when the `change` event occurs, re-run any effects that read `this.current`
const off = on(this.#query, 'change', update);
// stop listening when all the effects are destroyed
return () => off();
});
}
get current() {
this.#subscribe();
// Return the current state of the query, whether or not we're in an effect
return this.#query.matches;
}
}
createSubscriber
} from 'svelte/reactivity';
MediaQuery
自 5.7.0 起可用
¥Available since 5.7.0
创建媒体查询并提供反映其是否匹配的 current
属性。
¥Creates a media query and provides a current
property that reflects whether or not it matches.
请谨慎使用 - 在服务器端渲染期间,无法知道正确的值应该是什么,可能会导致内容在水合时发生变化。如果你可以在 CSS 中使用媒体查询来实现相同的效果,请这样做。
¥Use it carefully — during server-side rendering, there is no way to know what the correct value should be, potentially causing content to change upon hydration. If you can use the media query in CSS to achieve the same effect, do that.
<script>
import { MediaQuery } from 'svelte/reactivity';
const large = new MediaQuery('min-width: 800px');
</script>
<h1>{large.current ? 'large screen' : 'small screen'}</h1>
class MediaQuery extends ReactiveValue<boolean> {…}
constructor(query: string, fallback?: boolean | undefined);
query
媒体查询字符串¥
query
A media query stringfallback
服务器的后备值¥
fallback
Fallback value for the server
SvelteDate
class SvelteDate extends Date {…}
constructor(...params: any[]);
#private;
SvelteMap
class SvelteMap<K, V> extends Map<K, V> {…}
constructor(value?: Iterable<readonly [K, V]> | null | undefined);
set(key: K, value: V): this;
#private;
SvelteSet
class SvelteSet<T> extends Set<T> {…}
constructor(value?: Iterable<T> | null | undefined);
add(value: T): this;
#private;
SvelteURL
class SvelteURL extends URL {…}
get searchParams(): SvelteURLSearchParams;
#private;
SvelteURLSearchParams
class SvelteURLSearchParams extends URLSearchParams {…}
[REPLACE](params: URLSearchParams): void;
#private;
createSubscriber
自 5.7.0 起可用
¥Available since 5.7.0
返回一个 subscribe
函数,如果在效果(包括模板中的表达式)中调用该函数,则使用 update
函数调用其 start
回调。每当调用 update
时,效果都会重新运行。
¥Returns a subscribe
function that, if called in an effect (including expressions in the template),
calls its start
callback with an update
function. Whenever update
is called, the effect re-runs.
如果 start
返回一个函数,则当效果被销毁时将调用该函数。
¥If start
returns a function, it will be called when the effect is destroyed.
如果在多个效果中调用 subscribe
,则只要效果处于活动状态,start
将仅被调用一次,并且只有在所有效果都被销毁时才会调用返回的拆卸函数。
¥If subscribe
is called in multiple effects, start
will only be called once as long as the effects
are active, and the returned teardown function will only be called when all effects are destroyed.
最好通过一个例子来理解。以下是 MediaQuery
的实现:
¥It’s best understood with an example. Here’s an implementation of MediaQuery
:
import { function createSubscriber(start: (update: () => void) => (() => void) | void): () => void
Returns a subscribe
function that, if called in an effect (including expressions in the template),
calls its start
callback with an update
function. Whenever update
is called, the effect re-runs.
If start
returns a function, it will be called when the effect is destroyed.
If subscribe
is called in multiple effects, start
will only be called once as long as the effects
are active, and the returned teardown function will only be called when all effects are destroyed.
It’s best understood with an example. Here’s an implementation of MediaQuery
:
import { createSubscriber } from 'svelte/reactivity';
import { on } from 'svelte/events';
export class MediaQuery {
#query;
#subscribe;
constructor(query) {
this.#query = window.matchMedia(`(${query})`);
this.#subscribe = createSubscriber((update) => {
// when the `change` event occurs, re-run any effects that read `this.current`
const off = on(this.#query, 'change', update);
// stop listening when all the effects are destroyed
return () => off();
});
}
get current() {
this.#subscribe();
// Return the current state of the query, whether or not we're in an effect
return this.#query.matches;
}
}
createSubscriber } from 'svelte/reactivity';
import { function on<Type extends keyof WindowEventMap>(window: Window, type: Type, handler: (this: Window, event: WindowEventMap[Type]) => any, options?: AddEventListenerOptions | undefined): () => void (+4 overloads)
Attaches an event handler to the window and returns a function that removes the handler. Using this
rather than addEventListener
will preserve the correct order relative to handlers added declaratively
(with attributes like onclick
), which use event delegation for performance reasons
on } from 'svelte/events';
export class class MediaQuery
MediaQuery {
#query;
#subscribe;
constructor(query: any
query) {
this.#query = var window: Window & typeof globalThis
window.function matchMedia(query: string): MediaQueryList
matchMedia(`(${query: any
query})`);
this.#subscribe = function createSubscriber(start: (update: () => void) => (() => void) | void): () => void
Returns a subscribe
function that, if called in an effect (including expressions in the template),
calls its start
callback with an update
function. Whenever update
is called, the effect re-runs.
If start
returns a function, it will be called when the effect is destroyed.
If subscribe
is called in multiple effects, start
will only be called once as long as the effects
are active, and the returned teardown function will only be called when all effects are destroyed.
It’s best understood with an example. Here’s an implementation of MediaQuery
:
import { createSubscriber } from 'svelte/reactivity';
import { on } from 'svelte/events';
export class MediaQuery {
#query;
#subscribe;
constructor(query) {
this.#query = window.matchMedia(`(${query})`);
this.#subscribe = createSubscriber((update) => {
// when the `change` event occurs, re-run any effects that read `this.current`
const off = on(this.#query, 'change', update);
// stop listening when all the effects are destroyed
return () => off();
});
}
get current() {
this.#subscribe();
// Return the current state of the query, whether or not we're in an effect
return this.#query.matches;
}
}
createSubscriber((update: () => void
update) => {
// when the `change` event occurs, re-run any effects that read `this.current`
const const off: () => void
off = on<MediaQueryList, "change">(element: MediaQueryList, type: "change", handler: (this: MediaQueryList, event: MediaQueryListEvent) => any, options?: AddEventListenerOptions | undefined): () => void (+4 overloads)
Attaches an event handler to an element and returns a function that removes the handler. Using this
rather than addEventListener
will preserve the correct order relative to handlers added declaratively
(with attributes like onclick
), which use event delegation for performance reasons
on(this.#query, 'change', update: () => void
update);
// stop listening when all the effects are destroyed
return () => const off: () => void
off();
});
}
get MediaQuery.current: boolean
current() {
this.#subscribe();
// Return the current state of the query, whether or not we're in an effect
return this.#query.MediaQueryList.matches: boolean
matches;
}
}
function createSubscriber(
start: (update: () => void) => (() => void) | void
): () => void;