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
Copy file name to clipboardExpand all lines: src/guide/custom-directive.md
+45-45Lines changed: 45 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# Custom Directives
1
+
# カスタムディレクティブ
2
2
3
-
## Intro
3
+
## 基本
4
4
5
-
In addition to the default set of directives shipped in core (like `v-model`or`v-show`), Vue also allows you to register your own custom directives. Note that in Vue, the primary form of code reuse and abstraction is components - however, there may be cases where you need some low-level DOM access on plain elements, and this is where custom directives would still be useful. An example would be focusing on an input element, like this one:
When the page loads, that element gains focus (note: `autofocus` doesn't work on mobile Safari). In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Also, you can click on the `Rerun`button and input will be focused.
A directive definition object can provide several hook functions (all optional):
51
+
ディレクティブの定義オブジェクトは、いくつかのフック関数を提供しています (全てオプション):
52
52
53
-
-`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.
You can check the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `prevVnode`) in [Custom Directive API](../api/application-api.html#directive)
Directive arguments can be dynamic. For example, in `v-mydirective:[argument]="value"`, the `argument`can be updated based on data properties in our component instance! This makes our custom directives flexible for use throughout our application.
73
+
ディレクティブの引数は動的にできます。例えば、`v-mydirective:[argument]="value"` において、`argument`はコンポーネントインスタンスの data プロパティに基づいて更新されます! これにより、私たちのアプリケーション全体を通した利用に対して、カスタムディレクティブは柔軟になります。
74
74
75
-
Let's say you want to make a custom directive that allows you to pin elements to your page using fixed positioning. We could create a custom directive where the value updates the vertical positioning in pixels, like this:
This would pin the element 200px from the top of the page. But what happens if we run into a scenario when we need to pin the element from the left, instead of the top? Here's where a dynamic argument that can be updated per component instance comes in very handy:
Our custom directive is now flexible enough to support a few different use cases. To make it even more dynamic, we can also allow to modify a bound value. Let's create an additional property `pinPadding`and bind it to the `<input type="range">`
In previous example, you may want the same behavior on `mounted`and`updated`, but don't care about the other hooks. You can do it by passing the callback to directive:
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
In 3.0, with fragments support, components can potentially have more than one root nodes. This creates an issue when a custom directive is used on a component with multiple root nodes.
To explain the details of how custom directives will work on components in 3.0, we need to first understand how custom directives are compiled in 3.0. For a directive like this:
`withDirectives`returns a cloned VNode with the user hooks wrapped and injected as VNode lifecycle hooks (see [Render Function](render-function.html) for more details):
**As a result, custom directives are fully included as part of a VNode's data. When a custom directive is used on a component, these `onVnodeXXX`hooks are passed down to the component as extraneous props and end up in `this.$attrs`.**
This also means it's possible to directly hook into an element's lifecycle like this in the template, which can be handy when a custom directive is too involved:
This is consistent with the [attribute fallthrough behavior](component-attrs.html). So, the rule for custom directives on a component will be the same as other extraneous attributes: it is up to the child component to decide where and whether to apply it. When the child component uses `v-bind="$attrs"`on an inner element, it will apply any custom directives used on it as well.
0 commit comments