You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/v0.1.0/en/api/global/component.mdx
+87-43Lines changed: 87 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,22 @@ import! {
34
34
</script>
35
35
```
36
36
37
-
## `#[prop]`
37
+
## `#[component]`
38
+
39
+
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]`.
40
+
41
+
```rust
42
+
<script>
43
+
#[component]
44
+
pubstructMyView{
45
+
name:String
46
+
}
47
+
</script>
48
+
```
38
49
39
-
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.
50
+
## `#[prop]`
40
51
52
+
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.
41
53
### Limitations
42
54
43
55
**Not all types can be used as `prop`. Only types that implement the `Default` trait can be used for property declaration.**
@@ -46,60 +58,81 @@ The `#[prop]` macro is used to define the properties (Props) of a component. For
46
58
47
59
```rust
48
60
<script>
61
+
#[component]
62
+
pubstructMyView{
63
+
user:User,
64
+
auth:Auth
65
+
}
66
+
49
67
#[prop]
50
-
pubstructMyView {
51
-
name:String,
68
+
#[derive(Debug, Clone)]
69
+
pubstructUser{
70
+
name:String
71
+
}
72
+
73
+
implDefaultforUser{
74
+
fndefault() ->Self{
75
+
Self{ name:"John" }
76
+
}
77
+
}
78
+
79
+
#[prop]
80
+
#[derive(Default, Clone, Copy, Debug)]
81
+
pubenumAuth{
82
+
#[default]
83
+
Custom,
84
+
User,
85
+
Remote,
86
+
Other
52
87
}
53
88
</script>
54
89
```
55
90
56
91
### `get|set` method
57
92
58
-
All `#[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.
93
+
All 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.
59
94
60
95
For example, the above `MyView` structure will automatically generate the following methods:
61
96
62
-
-`fn get_name(&self) -> String;`
63
-
-`fn set_name(&mut self, value: String) -> ();`
64
-
97
+
-`fn get_user(&self) -> User;`
98
+
-`fn set_user(&mut self, value: User) -> ();`
99
+
-`fn get_auth(&self) -> Auth;`
100
+
-`fn set_auth(&self, value: Auth) -> ();`
65
101
66
-
## `default_prop!`
102
+
## `Default trait`
67
103
68
-
The `default_prop!` macro is used to initialize the property values of a component instance.
104
+
The `Default` trait is used to initialize the property values of a component instance.
69
105
70
106
### Example
71
107
72
108
```rust {7-11}
73
109
<script>
74
-
#[prop]
110
+
#[component]
75
111
pubstructMyView {
76
112
name:String,
77
113
}
78
114
79
-
letmutprop=default_prop! {
80
-
MyView {
81
-
name:"John".to_string(),
115
+
implDefaultforMyView{
116
+
fndefault() ->Self{
117
+
Self{
118
+
name:"John".to_string(),
119
+
}
82
120
}
83
-
};
121
+
}
84
122
</script>
85
123
```
86
124
87
-
In subsequent code, you can use `prop` to access `get|set` methods, for example:
125
+
In subsequent code, you can implement component methods and callback functions just like implementing impl, for example:
88
126
89
127
```rust
90
-
letname=prop.get_name();
91
-
prop.set_name("Alice".to_string());
128
+
implMyView{
129
+
fnclick_btn(&mutself) -> (){
130
+
letname=self.get_name();
131
+
self.set_name("Alice".to_string());
132
+
}
133
+
}
92
134
```
93
135
94
-
> [!DANGER]
95
-
>
96
-
> The `get|set` methods of `prop` are currently not supported in `macro_expr` (macro expression), for example:
97
-
>
98
-
> ```rust
99
-
> println!("name is: {}", prop.get_name());
100
-
> ```
101
-
102
-
103
136
## `#[event]`
104
137
105
138
Custom components do not trigger any events by default. To use the event system, you need to use the `#[event]` macro to declare events.
@@ -142,10 +175,15 @@ The `c_ref!` macro is used to obtain a component reference based on the componen
0 commit comments