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
A very simple way to attach javascript to the DOM. When even [petite-vue](https://github.com/vuejs/petite-vue) or [alpine.js](https://github.com/alpinejs/alpine/) would be too much.
3
+
A very simple way to attach javascript/typescript to the DOM. When even [petite-vue](https://github.com/vuejs/petite-vue) or [alpine.js](https://github.com/alpinejs/alpine/) would be too much.
Define properties to automatically convert `el.dataset` properties to the
106
-
correct type and enable autocompletion.
153
+
`props`, passed to the component's setup function can read from / write to the component elements dataset. By default all values are strings (as is the normal behavior with an element's dataset). But by providing types and default values for props, these values will be automatically converted to the correct type!
107
154
108
155
```ts
109
-
// You can either define a prop's type by proving a constructor ...
110
-
defineProps({ answer:Number, enabled:Boolean })
156
+
const options =defineOptions({
157
+
props: { count: 0 }
158
+
})
111
159
112
-
// ... or by providing a default value.
113
-
defineProps({ answer: 42, enabled: true })
160
+
registerComponent('my-component', options, ({ el, props }) => {
161
+
// If the element hasn't `data-count` specified, this will output the default
162
+
// value `0`.
163
+
console.log(props.count) // => 0
114
164
115
-
// For objects and arrays the default value can be wrapped inside a function
116
-
defineProps({ list: () => [1, 2, 3] })
165
+
// If the element has `data-count="20"`, "20" will be automatically parsed
166
+
// into a number and returned.
167
+
console.log(props.count) // => 20
168
+
})
117
169
```
118
170
119
-
Example:
171
+
This also works for complex data types:
120
172
121
173
```ts
122
-
const props =defineProps({
123
-
enabled: true,
124
-
message: String,
125
-
tags: () => ['default']
174
+
const options =defineOptions({
175
+
props: { todos: [] asstring[] }
126
176
})
127
177
128
-
registerComponent('my-component', ({ el }) => {
129
-
const { enabled, message, tags } =props(el)
130
-
})
131
-
```
178
+
// Lets say the html for the component looks like this:
Components try to stay as close to native APIs as possible. Therefore events are just [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent), but they can be fully typed:
265
+
266
+
```ts
267
+
const options =defineOptions({
268
+
events: { updateCounter: Number, close: null }
269
+
})
270
+
271
+
registerComponent('my-component', options, ({ el, ComponentEvent }) => {
272
+
// These will be autocompleted and generate type-errors if you forget, for
273
+
// example, the value for the `updateCounter` event.
0 commit comments