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/_guide/decorators.md
+45-23Lines changed: 45 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,51 @@ class HelloWorldElement extends HTMLElement {
37
37
38
38
Class Field decorators get given the class and the field name so they can add custom functionality to the field. Because they operate on the fields, they must be put on top of or to the left of the field.
39
39
40
-
#### Supporting `strictPropertyInitialization`
40
+
### Method Decorators
41
+
42
+
Method decorators work just like Field Decorators. Put them on top or on the left of the method, like so:
43
+
44
+
45
+
```js
46
+
classHelloWorldElementextendsHTMLElement {
47
+
48
+
@log
49
+
submit() {
50
+
// ...
51
+
}
52
+
53
+
// Alternative style
54
+
55
+
@log load() {
56
+
// ...
57
+
}
58
+
59
+
}
60
+
```
61
+
62
+
### Getter/Setter
63
+
64
+
Decorators can also be used over a `get` or `set` field. These work just like Field Decorators, but you can put them over one or both the `get` or `set` field. Some decorators might throw an error if you put them over a `get` field, when they expect to be put over a `set` field:
65
+
66
+
67
+
```js
68
+
classHelloWorldElementextendsHTMLElement {
69
+
70
+
@target setsomething() {
71
+
// ...
72
+
}
73
+
74
+
// Can be used over just one field
75
+
@attr getdata() {
76
+
return {}
77
+
}
78
+
setdata() {
79
+
80
+
}
81
+
}
82
+
```
83
+
84
+
### Supporting `strictPropertyInitialization`
41
85
42
86
TypeScript comes with various "strict" mode settings, one of which is `strictPropertyInitialization` which lets TypeScript catch potential class properties which might not be assigned during construction of a class. This option conflicts with Catalyst's `@target`/`@targets` decorators, which safely do the assignment but TypeScript's simple heuristics cannot detect this. There are two ways to work around this:
43
87
@@ -63,28 +107,6 @@ TypeScript comes with various "strict" mode settings, one of which is `strictPro
63
107
}
64
108
```
65
109
66
-
### MethodDecorators
67
-
68
-
Catalystdoesn't currently ship with any method decorators, but you might see them in code. They work just like Field Decorators (in fact they'rethesamething). Putthemontoporontheleftofthemethod, likeso:
69
-
70
-
71
-
```js
72
-
class HelloWorldElement extends HTMLElement {
73
-
74
-
@log
75
-
submit() {
76
-
// ...
77
-
}
78
-
79
-
// Alternative style
80
-
81
-
@log load() {
82
-
// ...
83
-
}
84
-
85
-
}
86
-
```
87
-
88
110
### FunctionCallingDecorators
89
111
90
112
Youmightseesomedecoratorsthatlooklikefunction calls, and that's because they are! Some decorators allow for customisation; callingwithadditionalarguments. Decoratorsthatexpecttobecalledaregenerallynotinterchangeablewiththenon-callvariant, adecoratorsdocumentationshouldtellyouhowtouseit.
0 commit comments