你正在查看 Lit 的旧版本文档。点击 这里查看最新版本。

Directives

A directive that renders the items of an async iterable[1], appending new values after previous values, similar to the built-in support for iterables.

导入

import { asyncAppend } from 'lit-html/directives/async-append.js';

签名

asyncAppend(value, mapper?): (part: Part) => Promise<void>

参数

value
AsyncIterable<T>

An async iterable

mapper?
(v: T, index?: number) => unknown

An optional function that maps from (value, index) to another value. Useful for generating templates for each item in the iterable.

详情

Async iterables are objects with a [Symbol.asyncIterator] method, which returns an iterator who's next() method returns a Promise. When a new value is available, the Promise resolves and the value is appended to the Part controlled by the directive. If another value other than this directive has been set on the Part, the iterable will no longer be listened to and new values won't be written to the Part.

A directive that renders the items of an async iterable[1], replacing previous values with new values, so that only one value is ever rendered at a time.

导入

import { asyncReplace } from 'lit-html/directives/async-replace.js';

签名

asyncReplace(value, mapper?): (part: Part) => Promise<void>

参数

value
AsyncIterable<T>

An async iterable

mapper?
(v: T, index?: number) => unknown

An optional function that maps from (value, index) to another value. Useful for generating templates for each item in the iterable.

详情

Async iterables are objects with a [Symbol.asyncIterator] method, which returns an iterator who's next() method returns a Promise. When a new value is available, the Promise resolves and the value is rendered to the Part controlled by the directive. If another value other than this directive has been set on the Part, the iterable will no longer be listened to and new values won't be written to the Part.

Enables fast switching between multiple templates by caching the DOM nodes and TemplateInstances produced by the templates.

导入

import { cache } from 'lit-html/directives/cache.js';

签名

cache(value): (part: Part) => void

参数

value
unknown

详情

Example:

let checked = false;

html`
${cache(checked ? html`input is checked` : html`input is not checked`)}
`

A directive that applies CSS classes. This must be used in the class attribute and must be the only part used in the attribute. It takes each property in the classInfo argument and adds the property name to the element's class if the property value is truthy; if the property value is falsey, the property name is removed from the element's class. For example {foo: bar} applies the class foo if the value of bar is truthy.

导入

import { classMap } from 'lit-html/directives/class-map.js';

签名

classMap(classInfo): (part: Part) => void

参数

classInfo
ClassInfo

导入

import { ClassInfo } from 'lit-html/directives/class-map.js';

Prevents re-render of a template function until a single value or an array of values changes.

导入

import { guard } from 'lit-html/directives/guard.js';

签名

guard(value, f): (part: Part) => void

参数

value
unknown

the value to check before re-rendering

f
() => unknown

the template function

详情

Example:

html`
<div>
${guard([user.id, company.id], () => html`...`)}
</div>

In this case, the template only renders if either user.id or company.id changes.

guard() is useful with immutable data patterns, by preventing expensive work until data updates.

Example:

html`
<div>
${guard([immutableItems], () => immutableItems.map(i => html`${i}`))}
</div>

In this case, items are mapped over only when the array reference changes.

For AttributeParts, sets the attribute if the value is defined and removes the attribute if the value is undefined.

导入

import { ifDefined } from 'lit-html/directives/if-defined.js';

签名

ifDefined(value): (part: Part) => void

参数

value
unknown

详情

For other part types, this directive is a no-op.

Checks binding values against live DOM values, instead of previously bound values, when determining whether to update the value.

导入

import { live } from 'lit-html/directives/live.js';

签名

live(value): (part: AttributePart | PropertyPart | BooleanAttributePart) => void

参数

value
unknown

详情

This is useful for cases where the DOM value may change from outside of lit-html, such as with a binding to an <input> element's value property, a content editable elements text, or to a custom element that changes it's own properties or attributes.

In these cases if the DOM value changes, but the value set through lit-html bindings hasn't, lit-html won't know to update the DOM value and will leave it alone. If this is not what you want—if you want to overwrite the DOM value with the bound value no matter what—use the live() directive:

html<input .value=${live(x)}>

live() performs a strict equality check agains the live DOM value, and if the new value is equal to the live value, does nothing. This means that live() should not be used when the binding will cause a type conversion. If you use live() with an attribute binding, make sure that only strings are passed in, or the binding will update every render.

A directive that repeats a series of values (usually TemplateResults) generated from an iterable, and updates those items efficiently when the iterable changes based on user-provided keys associated with each item.

导入

import { repeat } from 'lit-html/directives/repeat.js';

签名

repeat(items, keyFnOrTemplate, template?): DirectiveFn

参数

items
Iterable<T>
keyFnOrTemplate
KeyFn<T> | ItemTemplate<T>
template?
ItemTemplate<T>

详情

Note that if a keyFn is provided, strict key-to-DOM mapping is maintained, meaning previous DOM for a given key is moved into the new position if needed, and DOM will never be reused with values for different keys (new DOM will always be created for new keys). This is generally the most efficient way to use repeat since it performs minimum unnecessary work for insertions and removals.

IMPORTANT: If providing a keyFn, keys must be unique for all items in a given call to repeat. The behavior when two or more items have the same key is undefined.

If no keyFn is provided, this directive will perform similar to mapping items to values, and DOM will be reused against potentially different items.

导入

import { ItemTemplate } from 'lit-html/directives/repeat.js';

类型

(item: T, index: number) => unknown

导入

import { KeyFn } from 'lit-html/directives/repeat.js';

类型

(item: T, index: number) => unknown

A directive that applies CSS properties to an element.

导入

import { styleMap } from 'lit-html/directives/style-map.js';

签名

styleMap(styleInfo): (part: Part) => void

参数

styleInfo
StyleInfo

详情

styleMap can only be used in the style attribute and must be the only expression in the attribute. It takes the property names in the styleInfo object and adds the property values as CSS properties. Property names with dashes (-) are assumed to be valid CSS property names and set on the element's style object using setProperty(). Names without dashes are assumed to be camelCased JavaScript property names and set on the element's style object using property assignment, allowing the style object to translate JavaScript-style names to CSS property names.

For example styleMap({backgroundColor: 'red', 'border-top': '5px', '--size': '0'}) sets the background-color, border-top and --size properties.

导入

import { StyleInfo } from 'lit-html/directives/style-map.js';

Renders the content of a template element as HTML.

导入

import { templateContent } from 'lit-html/directives/template-content.js';

签名

templateContent(template): (part: Part) => void

参数

template
MDN HTMLTemplateElement

详情

Note, the template should be developer controlled and not user controlled. Rendering a user-controlled template with this directive could lead to cross-site-scripting vulnerabilities.

Renders the result as HTML, rather than text.

导入

import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';

签名

unsafeHTML(value): (part: Part) => void

参数

value
unknown

详情

Note, this is unsafe to use with any user-provided input that hasn't been sanitized or escaped, as it may lead to cross-site-scripting vulnerabilities.

Renders the result as SVG, rather than text.

导入

import { unsafeSVG } from 'lit-html/directives/unsafe-svg.js';

签名

unsafeSVG(value): (part: Part) => void

参数

value
unknown

详情

Note, this is unsafe to use with any user-provided input that hasn't been sanitized or escaped, as it may lead to cross-site-scripting vulnerabilities.

Renders one of a series of values, including Promises, to a Part.

导入

import { until } from 'lit-html/directives/until.js';

签名

until(args): (part: Part) => void

参数

args
Array<unknown>

详情

Values are rendered in priority order, with the first argument having the highest priority and the last argument having the lowest priority. If a value is a Promise, low-priority values will be rendered until it resolves.

The priority of values can be used to create placeholder content for async data. For example, a Promise with pending content can be the first, highest-priority, argument, and a non_promise loading indicator template can be used as the second, lower-priority, argument. The loading indicator will render immediately, and the primary content will render when the Promise resolves.

Example:

const content = fetch('./content.txt').then(r => r.text()); html${until(content, html<span>Loading...</span>)}