This document introduces the core API of the component system, including component import, property declaration, event management, etc., to help developers build and manage custom components more efficiently.
import!
The import!
macro is used to import custom components so that they can be used in the current scope.
Import Rust components into your structure using import!
:
#[prop]
The #[prop]
macro is used to define the properties (Props) of a component. For custom components, we need to use the #[prop]
macro to declare properties so that they can be assigned when the component is instantiated.
Not all types can be used as prop
. Only types that implement the Default
trait can be used for property declaration.
get|set
methodAll #[prop]
marked properties will automatically generate corresponding get
and set
methods. It is recommended to use get|set
methods to access and modify property values, otherwise two-way binding may fail.
For example, the above MyView
structure will automatically generate the following methods:
fn get_name(&self) -> String;
fn set_name(&mut self, value: String) -> ();
default_prop!
The default_prop!
macro is used to initialize the property values of a component instance.
In subsequent code, you can use prop
to access get|set
methods, for example:
The get|set
methods of prop
are currently not supported in macro_expr
(macro expression), for example:
#[event]
Custom components do not trigger any events by default. To use the event system, you need to use the #[event]
macro to declare events.
#[event]
.Debug
and Clone
traits.In the above example, we define the MyViewEvent
event type, where:
Clicked
: a click event without parameters.Changed(String)
: a change event with a String
parameter.c_ref!
The c_ref!
macro is used to obtain a component reference based on the component id
, making it easier to directly manipulate the reference of a component instance.
active!
The active!
macro is used to trigger an event defined inside a component and pass it to an external component for callback processing.
MyView
ComponentFatherView
ComponentThe FatherView
component listens to the events of MyView
: