Skip to content

Commit 4b4fc27

Browse files
committed
update api
1 parent 9ec7604 commit 4b4fc27

File tree

4 files changed

+213
-119
lines changed

4 files changed

+213
-119
lines changed

docs/v0.1.0/en/api/global/component.mdx

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ import! {
3434
</script>
3535
```
3636

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+
pub struct MyView{
45+
name: String
46+
}
47+
</script>
48+
```
3849

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]`
4051

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.
4153
### Limitations
4254

4355
**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
4658

4759
```rust
4860
<script>
61+
#[component]
62+
pub struct MyView{
63+
user: User,
64+
auth: Auth
65+
}
66+
4967
#[prop]
50-
pub struct MyView {
51-
name: String,
68+
#[derive(Debug, Clone)]
69+
pub struct User{
70+
name: String
71+
}
72+
73+
impl Default for User{
74+
fn default() -> Self{
75+
Self{ name: "John" }
76+
}
77+
}
78+
79+
#[prop]
80+
#[derive(Default, Clone, Copy, Debug)]
81+
pub enum Auth{
82+
#[default]
83+
Custom,
84+
User,
85+
Remote,
86+
Other
5287
}
5388
</script>
5489
```
5590

5691
### `get|set` method
5792

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.
5994

6095
For example, the above `MyView` structure will automatically generate the following methods:
6196

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) -> ();`
65101

66-
## `default_prop!`
102+
## `Default trait`
67103

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.
69105

70106
### Example
71107

72108
```rust {7-11}
73109
<script>
74-
#[prop]
110+
#[component]
75111
pub struct MyView {
76112
name: String,
77113
}
78114

79-
let mut prop = default_prop! {
80-
MyView {
81-
name: "John".to_string(),
115+
impl Default for MyView{
116+
fn default() -> Self{
117+
Self{
118+
name: "John".to_string(),
119+
}
82120
}
83-
};
121+
}
84122
</script>
85123
```
86124

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:
88126

89127
```rust
90-
let name = prop.get_name();
91-
prop.set_name("Alice".to_string());
128+
impl MyView{
129+
fn click_btn(&mut self) -> (){
130+
let name = self.get_name();
131+
self.set_name("Alice".to_string());
132+
}
133+
}
92134
```
93135

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-
103136
## `#[event]`
104137

105138
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
142175
</template>
143176

144177
<script>
145-
fn get_label_text() {
146-
let label = c_ref!(my_label);
147-
let label_text = label.get_text();
148-
println!("label text is: {}", label_text);
178+
#[component]
179+
struct MyView{}
180+
181+
impl MyView{
182+
fn get_label_text(&mut self) {
183+
let label = c_ref!(my_label);
184+
let label_text = label.get_text();
185+
println!("label text is: {}", label_text);
186+
}
149187
}
150188
</script>
151189
```
@@ -166,12 +204,14 @@ pub enum MyViewEvent {
166204
Changed(String),
167205
}
168206

169-
fn click_view() {
170-
let _ = active!(MyViewEvent::Clicked);
171-
}
207+
impl MyView{
208+
fn click_view(&self) {
209+
let _ = active!(MyViewEvent::Clicked);
210+
}
172211

173-
fn change_view() {
174-
active!(MyViewEvent::Changed, "changed!".to_string());
212+
fn change_view(&self) {
213+
active!(MyViewEvent::Changed, "changed!".to_string());
214+
}
175215
}
176216
```
177217

@@ -190,13 +230,17 @@ The `FatherView` component listens to the events of `MyView`:
190230
</template>
191231

192232
<script>
193-
fn my_view_clicked() {
194-
dbg!("my_view_clicked!");
195-
}
233+
#[component]
234+
struct FatherView{}
196235
197-
fn my_view_changed(param: impl EventParam) {
198-
dbg!(param);
236+
impl FatherView{
237+
fn my_view_clicked(&self) {
238+
dbg!("my_view_clicked!");
239+
}
240+
241+
fn my_view_changed(&self, param: impl EventParam) {
242+
dbg!(param);
243+
}
199244
}
200245
</script>
201-
```
202-
246+
```

docs/v0.1.0/en/api/option/lifetime.mdx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,37 @@ import { Badge } from '@theme';
44

55
> [!WARNING]
66
>
7-
> Currently, the complete life cycle has not been fully implemented. It is an experimental function. Open `#[before_create]`
7+
> Currently, the complete life cycle has not been fully implemented. It is an experimental function. Open `#[before_mount]`, `#mounted`
88
9-
## `#[before_create]`
9+
## `#[before_mount]`
1010

11-
Called immediately after the component instance is initialized. Use the `#[before_create]` annotation on a method or closure.
11+
Called immediately after the component instance is initialized. Use the `#[before_mount]` annotation on a method or closure.
1212

1313
### Usage
1414

15-
#### Fn
16-
1715
```rust
18-
#[before_create]
19-
fn before_create(){
20-
println!("before create!");
16+
impl MyView{
17+
#[before_mount]
18+
fn before_mount(){
19+
println!("before mount!");
20+
}
2121
}
2222
```
2323

24-
#### Closure
24+
## `#[mounted]`
2525

26-
```rust
27-
#[before_create]
28-
let mut before_create = || {
29-
println!("before create!");
30-
};
31-
```
26+
Options called after instance initialization is complete
3227

33-
## `#[created]`
28+
### Usage
3429

35-
Options called after instance initialization is complete
30+
```rust
31+
impl MyView{
32+
#[mounted]
33+
fn mounted(){
34+
println!("mounted!");
35+
}
36+
}
37+
```
3638

3739
## `#[before_update]`
3840

0 commit comments

Comments
 (0)