Saltar al contenido
TE736 · Tipos · error

`<style>` fuera de un componente no tiene ámbito

Un `<style>` con ámbito pertenece al fn de componente que lo rodea — el sello de clase del fn es el ámbito de los selectores. En una sentencia de nivel superior o un método de `impl`/`trait` no hay componente, así que no hay ámbito. Añade `global` (un `<style global>` estático fuera de un componente se renderiza en su lugar) o muévelo a un fn de componente. Un cuerpo con `@()` lo reporta incluso con `global`: sus valores necesitan un elemento raíz de componente.

Why this fires

A scoped <style> belongs to the component fn that surrounds it: the fn's class stamp is what its selectors are scoped to. In a top-level statement or an impl/trait method there is no component, so there is no scope to belong to — and before this error existed, such a declaration either rendered raw CSS in place (static body) or crashed at runtime (a body with @(), whose compiler marker is not a real function).

use std::html::{style}

let s = <style>.x { color: red }</style>
// error[TE736]: `<style>` outside a component has no scope; add `global` or move it into a component

Fix it

Either opt out of scoping — a static <style global> outside a component is accepted and renders in place:

let s = <style global>body { margin: 0 }</style>

— or move the declaration into a component fn, where it hoists and scopes normally:

fn Card() -> View {
    <div class="card">
        <style>.card { color: red }</style>
    </div>
}

A body containing @(expr) reports TE736 outside a component even under global: its values need a component root element to ride, and there is none.

See also

  • TE731 — runtime children requires global.
  • TE737@() with no root element to carry its value.

Véase también

Buscar en Zolo

9 resultados

en