Skip to content

Commit c9b8954

Browse files
feat: added created hook to directives (#719)
* feat: added `created` hood to directives * Update src/api/application-api.md Co-authored-by: skirtle <[email protected]> * Update src/guide/custom-directive.md Co-authored-by: skirtle <[email protected]> * Update src/guide/migration/custom-directives.md Co-authored-by: skirtle <[email protected]> * fix: updated the beforeMount description Co-authored-by: skirtle <[email protected]>
1 parent 7e65f12 commit c9b8954

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/api/application-api.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In addition, since the `createApp` method returns the application instance itsel
2222
- **Returns:**
2323

2424
- The application instance if a `definition` argument was passed
25-
- The component definition if a `definition` argument was not passed
25+
- The component definition if a `definition` argument was not passed
2626

2727
- **Usage:**
2828

@@ -73,7 +73,7 @@ app.config = {...}
7373
- **Returns:**
7474

7575
- The application instance if a `definition` argument was passed
76-
- The directive definition if a `definition` argument was not passed
76+
- The directive definition if a `definition` argument was not passed
7777

7878
- **Usage:**
7979

@@ -88,6 +88,8 @@ const app = createApp({})
8888
// register
8989
app.directive('my-directive', {
9090
// Directive has a set of lifecycle hooks:
91+
// called before bound element's attributes or event listeners are applied
92+
created() {},
9193
// called before bound element's parent component is mounted
9294
beforeMount() {},
9395
// called when bound element's parent component is mounted
@@ -168,7 +170,7 @@ Apart from `el`, you should treat these arguments as read-only and never modify
168170

169171
- **Returns:**
170172

171-
- The application instance
173+
- The application instance
172174

173175
- **Usage:**
174176

@@ -224,10 +226,10 @@ app.mount('#my-app')
224226
- **Usage:**
225227

226228
Sets a value that can be injected into all components within the application. Components should use `inject` to receive the provided values.
227-
229+
228230
From a `provide`/`inject` perspective, the application can be thought of as the root-level ancestor, with the root component as its only child.
229231

230-
This method should not be confused with the [provide component option](options-composition.html#provide-inject) or the [provide function](composition-api.html#provide-inject) in the composition API. While those are also part of the same `provide`/`inject` mechanism, they are used to configure values provided by a component rather than an application.
232+
This method should not be confused with the [provide component option](options-composition.html#provide-inject) or the [provide function](composition-api.html#provide-inject) in the composition API. While those are also part of the same `provide`/`inject` mechanism, they are used to configure values provided by a component rather than an application.
231233

232234
Providing values via the application is especially useful when writing plugins, as plugins typically wouldn't be able to provide values using components. It is an alternative to using [globalProperties](application-config.html#globalproperties).
233235

@@ -300,7 +302,7 @@ setTimeout(() => app.unmount('#my-app'), 5000)
300302
- **Usage:**
301303

302304
Install a Vue.js plugin. If the plugin is an Object, it must expose an `install` method. If it is a function itself, it will be treated as the install method.
303-
305+
304306
The install method will be called with the application as its first argument. Any `options` passed to `use` will be passed on in subsequent arguments.
305307

306308
When this method is called on the same plugin multiple times, the plugin will be installed only once.

src/guide/custom-directive.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ Then in a template, you can use the new `v-focus` attribute on any element, like
4545

4646
A directive definition object can provide several hook functions (all optional):
4747

48-
- `beforeMount`: called when the directive is first bound to the element and before parent component is mounted. This is where you can do one-time setup work.
48+
- `created`: called before the bound element's attributes or event listeners are applied. This is useful in cases where the directive needs to attach event listeners that must be called before normal `v-on` event listeners.
49+
50+
- `beforeMount`: called when the directive is first bound to the element and before parent component is mounted.
4951

5052
- `mounted`: called when the bound element's parent component is mounted.
5153

src/guide/migration/custom-directives.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Here, in the initial setup for this element, the directive binds a style by pass
3939

4040
In Vue 3, however, we’ve created a more cohesive API for custom directives. As you can see, they differ greatly from our component lifecycle methods even though we’re hooking into similar events. We’ve now unified them like so:
4141

42+
- **created** - new! This is called before the element's attributes or event listeners are applied.
4243
- bind → **beforeMount**
4344
- inserted → **mounted**
4445
- **beforeUpdate**: new! This is called before the element itself is updated, much like the component lifecycle hooks.

0 commit comments

Comments
 (0)