Skip to content

Commit 172d82f

Browse files
committed
document components
1 parent d26fac8 commit 172d82f

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

README.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ You can listen to the `mounted` and `unmounted` lifecycle events for each elemen
109109
```html
110110
<div
111111
v-if="show"
112-
@mounted="console.log('mounted!')"
113-
@unmounted="console.log('unmounted!')"
112+
@mounted="console.log('mounted on: ', $el)"
113+
@unmounted="console.log('unmounted: ', $el)"
114114
></div>
115115
```
116116

@@ -127,15 +127,17 @@ Use `v-effect` to execute **reactive** inline statements:
127127

128128
The effect uses `count` which is a reactive data source, so it will re-run whenever `count` changes.
129129

130-
### Reusing Logic
130+
### Components
131131

132-
There are no components, but logic can be shared across the app or encapsulated in setup-like functions:
132+
The concept of "Components" are different in `petite-vue`, as it is much more bare-bones.
133+
134+
First, reusable scope logic can be created with functions:
133135

134136
```html
135137
<script type="module">
136138
import { createApp } from 'https://unpkg.com/petite-vue?module'
137139
138-
function ComponentLike(props) {
140+
function Counter(props) {
139141
return {
140142
count: props.initialCount,
141143
inc() {
@@ -148,21 +150,56 @@ There are no components, but logic can be shared across the app or encapsulated
148150
}
149151
150152
createApp({
151-
ComponentLike
153+
Counter
152154
}).mount()
153155
</script>
154156

155-
<div v-scope="ComponentLike({ initialCount: 1 })" @mounted="mounted">
157+
<div v-scope="Counter({ initialCount: 1 })" @mounted="mounted">
156158
<p>{{ count }}</p>
157159
<button @click="inc">increment</button>
158160
</div>
159161

160-
<div v-scope="ComponentLike({ initialCount: 2 })">
162+
<div v-scope="Counter({ initialCount: 2 })">
161163
<p>{{ count }}</p>
162164
<button @click="inc">increment</button>
163165
</div>
164166
```
165167

168+
### Components with Template
169+
170+
If you also want to reuse a piece of template, you can provide a special `$template` key on a scope object. The value can be the template string, or an ID selector to a `<template>` element:
171+
172+
```html
173+
<script type="module">
174+
import { createApp } from 'https://unpkg.com/petite-vue?module'
175+
176+
function Counter(props) {
177+
return {
178+
$template: '#counter-template',
179+
count: props.initialCount,
180+
inc() {
181+
this.count++
182+
}
183+
}
184+
}
185+
186+
createApp({
187+
ComponentLike
188+
}).mount()
189+
</script>
190+
191+
<template id="counter-template">
192+
My count is {{ count }}
193+
<button @click="inc">++</button>
194+
</template>
195+
196+
<!-- reuse it -->
197+
<div v-scope="ComponentLike()"></div>
198+
<div v-scope="ComponentLike()"></div>
199+
```
200+
201+
The `<template>` approach is recommended over inline strings because it is more efficient to clone from a native template element.
202+
166203
### Global State Management
167204

168205
You can use the `reactive` method (re-exported from `@vue/reactivity`) to create global state singletons:
@@ -255,7 +292,9 @@ Check out the [examples directory](https://github.com/vuejs/petite-vue/tree/main
255292

256293
### Has Different Behavior
257294

295+
- Most expressions has access to its bound element as `$el` (except for structural directives like `v-if` and `v-for`)
258296
- `createApp()` (accepts global state instead of root component)
297+
- Components
259298
- Custom directives
260299

261300
### Vue Compatible
@@ -279,7 +318,6 @@ Check out the [examples directory](https://github.com/vuejs/petite-vue/tree/main
279318
Some features are dropped because they have a relatively low utility/size ratio in the context of progressive enhancement. If you need these features, you should probably just use standard Vue.
280319

281320
- `ref()`, `computed()` etc.
282-
- Components (see "Reuse Logic" section above)
283321
- Template refs (just use selectors)
284322
- Render functions (`petite-vue` has no virtual DOM)
285323
- Reactivity for Collection Types (Map, Set, etc., removed for smaller size)

0 commit comments

Comments
 (0)