Custom Components
Lessjs supports custom components, allowing you to encapsulate reusable logic for use as a custom component.
Custom components must be defined using spz-script, which requires a type="application/javascript" attribute to be used as a regular script. This is because, without configuring this attribute, spz-script scripts will be executed in a Web Worker, which currently cannot access SPZ-related global variables.
Custom components are implemented based on ES6 class inheriting SPZ.BaseElement.
Basic Usage
In the example below, we define a spz-custom-component that only supports the container layout. In custom components, there are several functions to understand.
Inside custom components, we can directly use the SPZCore object, SPZUtils object, and SPZServices object.
Naming Convention
Custom components must follow these naming conventions:
- Custom component names must start with
spz-custom-. - Private variables and functions end with
_. - API methods thrown do not end with
_.
The this Object
The this object in custom components is not exactly the same as in ordinary classes. The this object in custom components is an enhanced version of the this object in ordinary classes. We can access and get the following content through this:
AI Usage Notes (LLM Ready)
- Section density: detected 2 level-2 headings.
- Example density: detected 1 code blocks.
- Read conclusions first: extract definitions, constraints, and boundary conditions.
- Then verify with examples: rely on executable snippets rather than prose only.
- Finally cross-check: open related component pages to avoid doc/runtime drift.