跳至主要内容

编译器警告

如果 Svelte 捕获到潜在的错误(例如编写不可访问的标记),它会在编译时发出警告。

在您的具体用例中,某些警告可能不正确。您可以通过在导致警告的行上方放置一个 <!-- svelte-ignore <code> --> 注释来禁用此类误报。示例

<!-- svelte-ignore a11y_autofocus -->
<input autofocus />

您可以在单个注释中列出多个规则(用逗号分隔),并在旁边添加解释性说明(在括号中)

<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions (because of reasons) -->
<div onclick>...</div>

a11y_accesskey

Avoid using accesskey

强制元素上不使用 accesskey。访问键是 HTML 属性,允许 Web 开发人员为元素分配键盘快捷键。键盘快捷键与屏幕阅读器和仅键盘用户使用的键盘命令之间存在不一致,会导致辅助功能复杂化。为了避免复杂化,不应使用访问键。

<!-- A11y: Avoid using accesskey -->
<div accesskey="z"></div>

a11y_aria_activedescendant_has_tabindex

An element with an aria-activedescendant attribute should have a tabindex value

具有 aria-activedescendant 的元素必须可聚焦,因此它必须具有固有的 tabindex 或将 tabindex 声明为属性。

<!-- A11y: Elements with attribute aria-activedescendant should have tabindex value -->
<div aria-activedescendant="some-id"></div>

a11y_aria_attributes

`<%name%>` should not have aria-* attributes

某些保留的 DOM 元素不支持 ARIA 角色、状态和属性。这通常是因为它们不可见,例如 metahtmlscriptstyle。此规则强制这些 DOM 元素不包含 aria-* 属性。

<!-- A11y: <meta> should not have aria-* attributes -->
<meta aria-hidden="false" />

a11y_autocomplete_valid

'%value%' is an invalid value for 'autocomplete' on `<input type="%type%">`

a11y_autofocus

Avoid using autofocus

强制元素上不使用 autofocus。自动聚焦元素会导致视力和非视力用户都面临可用性问题。

<!-- A11y: Avoid using autofocus -->
<input autofocus />

a11y_click_events_have_key_events

Visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as `<button type="button">` or `<a>` might be more appropriate. See https://svelte.js.cn/docs/accessibility-warnings#a11y-click-events-have-key-events for more details

强制具有 onclick 事件的可见的非交互式元素伴随着键盘事件处理程序。

用户应首先考虑是否可以使用更合适的交互式元素,例如用于操作的 <button type="button"> 元素或用于导航的 <a> 元素。这些元素在语义上更有意义,并且将具有内置的键处理。例如,SpaceEnter 将触发 <button>,而 Enter 将触发 <a> 元素。

如果需要非交互式元素,则 onclick 应伴随着 onkeyuponkeydown 处理程序,这些处理程序允许用户通过键盘执行等效操作。为了让用户能够触发按键,还需要通过添加 tabindex 使元素可聚焦。虽然 onkeypress 处理程序也会消除此警告,但需要注意的是 keypress 事件已弃用。

<!-- A11y: visible, non-interactive elements with an onclick event must be accompanied by a keyboard event handler. -->
<div onclick={() => {}}></div>

为键盘编写代码对于患有身体残疾无法使用鼠标的用户、辅助技术兼容性和屏幕阅读器用户非常重要。

a11y_consider_explicit_label

Buttons and links should either contain text or have an `aria-label` or `aria-labelledby` attribute

a11y_distracting_elements

Avoid `<%name%>` elements

强制不使用分散注意力的元素。视觉上分散注意力的元素会导致视力障碍用户的辅助功能问题。此类元素很可能已弃用,应避免使用。

以下元素在视觉上分散注意力:<marquee><blink>

<!-- A11y: Avoid <marquee> elements -->
<marquee></marquee>

a11y_figcaption_index

`<figcaption>` must be first or last child of `<figure>`

a11y_figcaption_parent

`<figcaption>` must be an immediate child of `<figure>`

强制某些 DOM 元素具有正确的结构。

<!-- A11y: <figcaption> must be an immediate child of <figure> -->
<div>
	<figcaption>Image caption</figcaption>
</div>

a11y_hidden

`<%name%>` element should not be hidden

某些 DOM 元素对屏幕阅读器导航很有用,不应隐藏。

<!-- A11y: <h2> element should not be hidden -->
<h2 aria-hidden="true">invisible header</h2>

a11y_img_redundant_alt

Screenreaders already announce `<img>` elements as an image

强制 img alt 属性不包含单词 image、picture 或 photo。屏幕阅读器已将 img 元素宣布为图像。无需使用诸如 imagephoto 和/或 picture 等词语。

<img src="foo" alt="Foo eating a sandwich." />

<!-- aria-hidden, won't be announced by screen reader -->
<img src="bar" aria-hidden="true" alt="Picture of me taking a photo of an image" />

<!-- A11y: Screen readers already announce <img> elements as an image. -->
<img src="foo" alt="Photo of foo being weird." />

<!-- A11y: Screen readers already announce <img> elements as an image. -->
<img src="bar" alt="Image of me at a bar!" />

<!-- A11y: Screen readers already announce <img> elements as an image. -->
<img src="foo" alt="Picture of baz fixing a bug." />

a11y_incorrect_aria_attribute_type

The value of '%attribute%' must be a %type%

强制仅对 aria 属性使用正确的类型的值。例如,aria-hidden 只能接收布尔值。

<!-- A11y: The value of 'aria-hidden' must be exactly one of true or false -->
<div aria-hidden="yes"></div>

a11y_incorrect_aria_attribute_type_boolean

The value of '%attribute%' must be either 'true' or 'false'. It cannot be empty

a11y_incorrect_aria_attribute_type_id

The value of '%attribute%' must be a string that represents a DOM element ID

a11y_incorrect_aria_attribute_type_idlist

The value of '%attribute%' must be a space-separated list of strings that represent DOM element IDs

a11y_incorrect_aria_attribute_type_integer

The value of '%attribute%' must be an integer

a11y_incorrect_aria_attribute_type_token

The value of '%attribute%' must be exactly one of %values%

a11y_incorrect_aria_attribute_type_tokenlist

The value of '%attribute%' must be a space-separated list of one or more of %values%

a11y_incorrect_aria_attribute_type_tristate

The value of '%attribute%' must be exactly one of true, false, or mixed

a11y_interactive_supports_focus

Elements with the '%role%' interactive role must have a tabindex value

强制具有交互式角色和交互式处理程序(鼠标或按键)的元素必须可聚焦或可选项卡。

<!-- A11y: Elements with the 'button' interactive role must have a tabindex value. -->
<div role="button" onkeypress={() => {}} />

a11y_invalid_attribute

'%href_value%' is not a valid %href_attribute% attribute

强制对辅助功能很重要的属性具有有效值。例如,href 不应为空、'#'javascript:

<!-- A11y: '' is not a valid href attribute -->
<a href="">invalid</a>

a11y_label_has_associated_control

A form label must be associated with a control

强制标签标签具有文本标签和关联的控件。

有两种支持的方法可以将标签与控件关联

  • 将控件包装在标签标签中。
  • 向标签添加 for 并将其分配给页面上输入的 ID。
<label for="id">B</label>

<label>C <input type="text" /></label>

<!-- A11y: A form label must be associated with a control. -->
<label>A</label>

a11y_media_has_caption

`<video>` elements must have a `<track kind="captions">`

为媒体提供字幕对于聋人用户跟随内容至关重要。字幕应该是对话、音效、相关音乐提示和其他相关音频信息的转录或翻译。这不仅对辅助功能很重要,而且在媒体不可用时(类似于图像无法加载时图像上的 alt 文本)对所有用户都有用。

字幕应包含理解相应媒体的所有重要和相关信息。这可能意味着字幕不是媒体内容中对话的 1:1 映射。但是,对于具有 muted 属性的视频组件,不需要字幕。

<video><track kind="captions" /></video>

<audio muted></audio>

<!-- A11y: Media elements must have a <track kind=\"captions\"> -->
<video></video>

<!-- A11y: Media elements must have a <track kind=\"captions\"> -->
<video><track /></video>

a11y_misplaced_role

`<%name%>` should not have role attribute

某些保留的 DOM 元素不支持 ARIA 角色、状态和属性。这通常是因为它们不可见,例如 metahtmlscriptstyle。此规则强制这些 DOM 元素不包含 role 属性。

<!-- A11y: <meta> should not have role attribute -->
<meta role="tooltip" />

a11y_misplaced_scope

The scope attribute should only be used with `<th>` elements

scope 属性只能用于 <th> 元素。

<!-- A11y: The scope attribute should only be used with <th> elements -->
<div scope="row" />

a11y_missing_attribute

`<%name%>` element should have %article% %sequence% attribute

强制元素上存在辅助功能所需的属性。这包括以下检查

  • <a> 应具有 href(除非它是 片段定义标签
  • <area> 应具有 alt、aria-label 或 aria-labelledby
  • <html> 应具有 lang
  • <iframe> 应具有 title
  • <img> 应具有 alt
  • <object> 应具有 title、aria-label 或 aria-labelledby
  • <input type="image"> 应具有 alt、aria-label 或 aria-labelledby
<!-- A11y: <input type=\"image\"> element should have an alt, aria-label or aria-labelledby attribute -->
<input type="image" />

<!-- A11y: <html> element should have a lang attribute -->
<html></html>

<!-- A11y: <a> element should have an href attribute -->
<a>text</a>

a11y_missing_content

`<%name%>` element should contain text

强制标题元素 (h1h2 等) 和锚点具有内容,并且该内容可供屏幕阅读器访问

<!-- A11y: <a> element should have child content -->
<a href="/foo"></a>

<!-- A11y: <h1> element should have child content -->
<h1></h1>

a11y_mouse_events_have_key_events

'%event%' event must be accompanied by '%accompanied_by%' event

强制要求onmouseoveronmouseout分别与onfocusonblur同时使用。这有助于确保通过这些鼠标事件触发的任何功能也能被键盘用户访问。

<!-- A11y: onmouseover must be accompanied by onfocus -->
<div onmouseover={handleMouseover} />

<!-- A11y: onmouseout must be accompanied by onblur -->
<div onmouseout={handleMouseout} />

a11y_no_abstract_role

Abstract role '%role%' is forbidden

a11y_no_interactive_element_to_noninteractive_role

`<%element%>` cannot have role '%role%'

不应使用WAI-ARIA角色将交互式元素转换为非交互式元素。非交互式ARIA角色包括articlebannercomplementaryimglistitemmainregiontooltip

<!-- A11y: <textarea> cannot have role 'listitem' -->
<textarea role="listitem"></textarea>

a11y_no_noninteractive_element_interactions

Non-interactive element `<%element%>` should not be assigned mouse or keyboard event listeners

非交互式元素不支持事件处理程序(鼠标和键盘处理程序)。非交互式元素包括<main><area><h1>(、<h2>等)、<p><img><li><ul><ol>。非交互式WAI-ARIA角色包括articlebannercomplementaryimglistitemmainregiontooltip

<!-- `A11y: Non-interactive element <li> should not be assigned mouse or keyboard event listeners.` -->
<li onclick={() => {}}></li>

<!-- `A11y: Non-interactive element <div> should not be assigned mouse or keyboard event listeners.` -->
<div role="listitem" onclick={() => {}}></div>

a11y_no_noninteractive_element_to_interactive_role

Non-interactive element `<%element%>` cannot have interactive role '%role%'

不应使用WAI-ARIA角色将非交互式元素转换为交互式元素。交互式ARIA角色包括buttonlinkcheckboxmenuitemmenuitemcheckboxmenuitemradiooptionradiosearchboxswitchtextbox

<!-- A11y: Non-interactive element <h3> cannot have interactive role 'searchbox' -->
<h3 role="searchbox">Button</h3>

a11y_no_noninteractive_tabindex

noninteractive element cannot have nonnegative tabIndex value

Tab键导航应仅限于页面上可以交互的元素。

<!-- A11y: noninteractive element cannot have nonnegative tabIndex value -->
<div tabindex="0"></div>

a11y_no_redundant_roles

Redundant role '%role%'

某些HTML元素具有默认的ARIA角色。为这些元素提供浏览器已设置的ARIA角色没有任何效果,这是多余的。

<!-- A11y: Redundant role 'button' -->
<button role="button">...</button>

<!-- A11y: Redundant role 'img' -->
<img role="img" src="foo.jpg" />

a11y_no_static_element_interactions

`<%element%>` with a %handler% handler must have an ARIA role

<div>这样的元素,如果带有像click这样的交互式处理程序,则必须具有ARIA角色。

<!-- A11y: <div> with click handler must have an ARIA role -->
<div onclick={() => ''}></div>

a11y_positive_tabindex

Avoid tabindex values above zero

避免使用正数的tabindex属性值。这会将元素移出预期的Tab顺序,为键盘用户造成混乱的体验。

<!-- A11y: avoid tabindex values above zero -->
<div tabindex="1"></div>

a11y_role_has_required_aria_props

Elements with the ARIA role "%role%" must have the following attributes defined: %props%

具有ARIA角色的元素必须具有该角色的所有必需属性。

<!-- A11y: A11y: Elements with the ARIA role "checkbox" must have the following attributes defined: "aria-checked" -->
<span role="checkbox" aria-labelledby="foo" tabindex="0"></span>

a11y_role_supports_aria_props

The attribute '%attribute%' is not supported by the role '%role%'

具有显式或隐式定义的角色的元素仅包含该角色支持的aria-*属性。

<!-- A11y: The attribute 'aria-multiline' is not supported by the role 'link'. -->
<div role="link" aria-multiline></div>

<!-- A11y: The attribute 'aria-required' is not supported by the role 'listitem'. This role is implicit on the element <li>. -->
<li aria-required></li>

a11y_role_supports_aria_props_implicit

The attribute '%attribute%' is not supported by the role '%role%'. This role is implicit on the element `<%name%>`

具有显式或隐式定义的角色的元素仅包含该角色支持的aria-*属性。

<!-- A11y: The attribute 'aria-multiline' is not supported by the role 'link'. -->
<div role="link" aria-multiline></div>

<!-- A11y: The attribute 'aria-required' is not supported by the role 'listitem'. This role is implicit on the element <li>. -->
<li aria-required></li>

a11y_unknown_aria_attribute

Unknown aria attribute 'aria-%attribute%'
Unknown aria attribute 'aria-%attribute%'. Did you mean '%suggestion%'?

强制仅使用已知的ARIA属性。这基于WAI-ARIA状态和属性规范

<!-- A11y: Unknown aria attribute 'aria-labeledby' (did you mean 'labelledby'?) -->
<input type="image" aria-labeledby="foo" />

a11y_unknown_role

Unknown role '%role%'
Unknown role '%role%'. Did you mean '%suggestion%'?

具有ARIA角色的元素必须使用有效的非抽象ARIA角色。角色定义的参考可以在WAI-ARIA网站上找到。

<!-- A11y: Unknown role 'toooltip' (did you mean 'tooltip'?) -->
<div role="toooltip"></div>

attribute_avoid_is

The "is" attribute is not supported cross-browser and should be avoided

attribute_global_event_reference

You are referencing `globalThis.%name%`. Did you forget to declare a variable with that name?

attribute_illegal_colon

Attributes should not contain ':' characters to prevent ambiguity with Svelte directives

attribute_invalid_property_name

'%wrong%' is not a valid HTML attribute. Did you mean '%right%'?

attribute_quoted

Quoted attributes on components and custom elements will be stringified in a future version of Svelte. If this isn't what you want, remove the quotes

bind_invalid_each_rest

The rest operator (...) will create a new object and binding '%name%' with the original object will not work

block_empty

Empty block

component_name_lowercase

`<%name%>` will be treated as an HTML element unless it begins with a capital letter

css_unused_selector

Unused CSS selector "%name%"

element_invalid_self_closing_tag

Self-closing HTML tags for non-void elements are ambiguous — use `<%name% ...></%name%>` rather than `<%name% ... />`

event_directive_deprecated

Using `on:%name%` to listen to the %name% event is deprecated. Use the event attribute `on%name%` instead

export_let_unused

Component has unused export property '%name%'. If it is for external reference only, please consider using `export const %name%`

legacy_code

`%code%` is no longer valid — please use `%suggestion%` instead

legacy_component_creation

Svelte 5 components are no longer classes. Instantiate them using `mount` or `hydrate` (imported from 'svelte') instead.

node_invalid_placement_ssr

%message%. When rendering this component on the server, the resulting HTML will be modified by the browser (by moving, removing, or inserting elements), likely resulting in a `hydration_mismatch` warning

HTML限制了某些元素可以出现的位置。如果发生违规,浏览器将以破坏Svelte对组件结构假设的方式“修复”HTML。一些示例

  • <p>hello <div>world</div></p>将导致<p>hello </p><div>world</div><p></p><div>自动关闭了<p>,因为<p>不能包含块级元素)
  • <option><div>option a</div></option>将导致<option>option a</option><div>被移除)
  • <table><tr><td>cell</td></tr></table>将导致<table><tbody><tr><td>cell</td></tr></tbody></table>(自动插入了一个<tbody>

当组件在客户端渲染时,此代码将正常工作(这就是为什么这是一个警告而不是错误的原因),但是如果您使用服务器渲染,它将导致水合失败。

non_reactive_update

`%name%` is updated, but is not declared with `$state(...)`. Changing its value will not correctly trigger updates

options_deprecated_accessors

The `accessors` option has been deprecated. It will have no effect in runes mode

options_deprecated_immutable

The `immutable` option has been deprecated. It will have no effect in runes mode

options_missing_custom_element

The `customElement` option is used when generating a custom element. Did you forget the `customElement: true` compile option?

options_removed_enable_sourcemap

The `enableSourcemap` option has been removed. Source maps are always generated now, and tooling can choose to ignore them

options_removed_hydratable

The `hydratable` option has been removed. Svelte components are always hydratable now

options_removed_loop_guard_timeout

The `loopGuardTimeout` option has been removed

options_renamed_ssr_dom

`generate: "dom"` and `generate: "ssr"` options have been renamed to "client" and "server" respectively

perf_avoid_inline_class

Avoid 'new class' — instead, declare the class at the top level scope

perf_avoid_nested_class

Avoid declaring classes below the top level scope

reactive_declaration_invalid_placement

Reactive declarations only exist at the top level of the instance script

reactive_declaration_module_script_dependency

Reassignments of module-level declarations will not cause reactive statements to update

script_context_deprecated

`context="module"` is deprecated, use the `module` attribute instead

script_unknown_attribute

Unrecognized attribute — should be one of `generics`, `lang` or `module`. If this exists for a preprocessor, ensure that the preprocessor removes it

slot_element_deprecated

Using `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead

state_referenced_locally

State referenced in its own scope will never update. Did you mean to reference it inside a closure?

store_rune_conflict

It looks like you're using the `$%name%` rune, but there is a local binding called `%name%`. Referencing a local variable with a `$` prefix will create a store subscription. Please rename `%name%` to avoid the ambiguity

svelte_component_deprecated

`<svelte:component>` is deprecated in runes mode — components are dynamic by default

在早期版本的Svelte中,组件构造函数在组件渲染时是固定的。换句话说,如果您希望<X>X发生更改时重新渲染,则必须使用<svelte:component this={X}>或将组件放在{#key X}...{/key}块中。

在Svelte 5中,情况不再如此——如果X发生更改,<X>将重新渲染。

在某些情况下,可以使用<object.property>语法作为替代;Svelte 5中将小写变量加上属性访问识别为组件。

对于复杂的组件解析逻辑,可能需要一个中间的大写变量。例如,在可以使用@const的地方

{#each items as item}
	<svelte:component this={item.condition ? Y : Z} />
	{@const Component = item.condition ? Y : Z}
	<Component />
{/each}

派生值可用于其他上下文中

<script>
	// ...
	let condition = $state(false);
	const Component = $derived(condition ? Y : Z);
</script>

<svelte:component this={condition ? Y : Z} />
<Component />

svelte_element_invalid_this

`this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte

svelte_self_deprecated

`<svelte:self>` is deprecated — use self-imports (e.g. `import %name% from './%basename%'`) instead

unknown_code

`%code%` is not a recognised code
`%code%` is not a recognised code (did you mean `%suggestion%`?)

在GitHub上编辑此页面

上一页 下一页