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!
:
#[component]
For custom components, we need to use the #[component]
macro to declare the component's properties, but not all types are allowed. Types that can be used in properties need to implement the Default
trait, and custom structs and enums need to be annotated with #[prop]
.
#[prop]
The #[prop]
macro is used to define the properties (Props) of a component. 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 properties declared in components and bound in templates will automatically generate corresponding get
and set
methods. Please use the get|set
methods to access and modify property values, otherwise the two-way binding will fail.
For example, the above MyView
structure will automatically generate the following methods:
fn get_user(&self) -> User;
fn set_user(&mut self, value: User) -> ();
fn get_auth(&self) -> Auth;
fn set_auth(&self, value: Auth) -> ();
Default trait
The Default
trait is used to initialize the property values of a component instance.
In subsequent code, you can implement component methods and callback functions just like implementing impl, 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
:
#[computed]
Computed properties are values dynamically calculated based on existing properties. In GenUI, computed properties do not store state, but are automatically updated based on changes in their dependencies.
Features | Computed | Two-way Binding |
---|---|---|
Store state? | ❌ No | ✅ Yes |
Mutable? | ✅ Update when dependencies change | ✅ Yes |
Manually editable? | ✅ But cannot use set_xxx method | ✅ Modify via set_xxx |
Dependent data | ✅ Requires dependencies | ✅ Component state |
#[computed]
needs to be declared in the method#[computed]
depends on the fields declared in the component, which can be multipleset_xxx()
cannot be used in the calculated property method to change the component field. Instead, a more flexible assignment change is used
$(arg)*
: Depends on the parameter list, indicating that there can be one or more$computed_fn
: Calculated attribute processing function$[&self|&mut self]?
: Indicates that it can be&self
or&mut self
, which is up to you to decide$return_value_type
: Return value type
The way to write a calculated property is to use a method to declare it when binding the property:
nav_to!
Used to jump from a sub-page of routing management to other sub-pages.
$page_id
: id of the page
The login key configured in router.toml
is the page id
nav_back!
Return the route to the previous page by pushing the stack, if any.