Skip to content

Commit 1d6295e

Browse files
committed
feat: add v-init
1 parent 7ac5766 commit 1d6295e

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ Or, use the ES modules build:
4242
<script>
4343
```
4444
45+
### `v-init`
46+
47+
Use `v-init` to execute one-off inline statements when an element is processed:
48+
49+
```html
50+
<div id="foo" v-init="console.log($el.id)"></div>
51+
```
52+
4553
### `v-effect`
4654
47-
Use `v-effect` to execute inline reactive statements:
55+
Use `v-effect` to execute **reactive** inline statements:
4856
4957
```html
5058
<div v-scope="{ count: 0 }">
@@ -53,6 +61,8 @@ Use `v-effect` to execute inline reactive statements:
5361
</div>
5462
```
5563
64+
The effect uses `count` which is a reactive data source, so it will re-run whenever `count` changes.
65+
5666
### Global Data
5767
5868
The `createApp` function accepts a data object that serves as the root scope for all expressions. This can be used for simple global state management:
@@ -161,6 +171,7 @@ const html = ({ el, get, effect }) => {
161171
### `petite-vue` only
162172

163173
- `v-scope`
174+
- `v-init`
164175
- `v-effect`
165176

166177
### Has Different Behavior

src/directives/bind.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ const setProp = (
7575
}
7676
}
7777
}
78-
} else if (key in el && !forceAttrRE.test(key)) {
78+
} else if (
79+
!(el instanceof SVGElement) &&
80+
key in el &&
81+
!forceAttrRE.test(key)
82+
) {
7983
// @ts-ignore
8084
el[key] = value
8185
if (key === 'value') {

src/directives/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { show } from './show'
66
import { text } from './text'
77
import { html } from './html'
88
import { model } from './model'
9+
import { init } from './init'
910
import { effect } from './effect'
1011

1112
export interface Directive<T = Element> {
@@ -29,5 +30,6 @@ export const builtInDirectives: Record<string, Directive<any>> = {
2930
text,
3031
html,
3132
model,
33+
init,
3234
effect
3335
}

src/directives/init.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Directive } from '.'
2+
3+
export const init: Directive = ({ get }) => {
4+
get()
5+
}

0 commit comments

Comments
 (0)