Skip to content

Commit 79f04f5

Browse files
committed
remove v-init, use lifecycle events
1 parent f417449 commit 79f04f5

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ Or, use the ES modules build:
4242
<script>
4343
```
4444
45-
### `v-init`
45+
### Lifecycle Events
4646
47-
Use `v-init` to execute one-off inline statements when an element is processed:
47+
You can listen to the `mounted` and `unmounted` lifecycle events for each element:
4848
4949
```html
50-
<div id="foo" v-init="console.log($el.id)"></div>
50+
<div
51+
v-if="show"
52+
@mounted="console.log('mounted!')"
53+
@unmounted="console.log('unmounted!')"
54+
></div>
5155
```
5256
5357
### `v-effect`
@@ -171,8 +175,8 @@ const html = ({ el, get, effect }) => {
171175
### `petite-vue` only
172176

173177
- `v-scope`
174-
- `v-init`
175178
- `v-effect`
179+
- `@mounted` & `@unmounted` events
176180

177181
### Has Different Behavior
178182

examples/todomvc.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
.mount('#app')
138138
</script>
139139

140-
<div id="app" v-init="setupRouting()" v-effect="save()" v-cloak>
140+
<div id="app" @mounted="setupRouting" v-effect="save()" v-cloak>
141141
<section class="todoapp">
142142
<header class="header">
143143
<h1>todos</h1>

src/directives/index.ts

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

1211
export interface Directive<T = Element> {
@@ -30,6 +29,5 @@ export const builtInDirectives: Record<string, Directive<any>> = {
3029
text,
3130
html,
3231
model,
33-
init,
3432
effect
3533
}

src/directives/init.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/directives/on.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Directive } from '.'
22
import { hyphenate } from '@vue/shared'
33
import { listen } from '../utils'
4+
import { nextTick } from '../scheduler'
45

56
// same as vue 2
67
const simplePathRE =
@@ -34,6 +35,14 @@ export const on: Directive = ({ el, get, exp, arg, modifiers }) => {
3435
? get(`(e => ${exp}(e))`)
3536
: get(`($event => { ${exp} })`)
3637

38+
// special lifecycle events
39+
if (arg === 'mounted') {
40+
nextTick(handler)
41+
return
42+
} else if (arg === 'unmounted') {
43+
return () => handler()
44+
}
45+
3746
if (modifiers) {
3847
// map modifiers
3948
if (arg === 'click') {

0 commit comments

Comments
 (0)