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
+40-36
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
---
2
-
title: Custom Directives
2
+
title: カスタムディレクティブ
3
3
type: guide
4
4
order: 16
5
5
---
6
6
7
-
## Intro
7
+
## 基本
8
8
9
-
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Note that in Vue 2.0, the primary form of code reuse and abstraction is components - however there may be cases where you just 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:
Vue 2.0 では、コードの再利用と抽象化における基本の形はコンポーネントです。 - しかしながら、通常の要素で低レベル DOM にアクセスしなければならないケースがあるかもしれません。こういった場面では、カスタムディレクティブが役立つでしょう。
11
+
ひとつの例として、以下のような input 要素へのフォーカスが挙げられます:
10
12
11
13
{% raw %}
12
14
<divid="simplest-directive-example"class="demo">
@@ -24,69 +26,71 @@ new Vue({
24
26
</script>
25
27
{% endraw %}
26
28
27
-
When the page loads, that element gains focus. In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Now let's build the directive that accomplishes this:
-`inserted`: called when the bound element has been inserted into its parent node (this only guarantees parent node presence, not necessarily in-document).
-`update`: called whenever the bound element's containing component is updated. The directive's value may or may not have changed. You can skip unnecessary updates by comparing the binding's current and old values (see below on hook arguments).
-**el**: The element the directive is bound to. This can be used to directly manipulate the DOM.
77
-
-**binding**: An object containing the following properties.
78
-
-**name**: The name of the directive, without the `v-` prefix.
79
-
-**value**: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be `2`.
80
-
-**oldValue**: The previous value, only available in `update` and `componentUpdated`. It is available whether or not the value has changed.
81
-
-**expression**: The expression of the binding as a string. For example in `v-my-directive="1 + 1"`, the expression would be `"1 + 1"`.
82
-
-**arg**: The argument passed to the directive, if any. For example in `v-my-directive:foo`, the arg would be `"foo"`.
83
-
-**modifiers**: An object containing modifiers, if any. For example in `v-my-directive.foo.bar`, the modifiers object would be `{ foo: true, bar: true }`.
84
-
-**vnode**: The virtual node produced by Vue's compiler. See the [VNode API](/api/#VNode-Interface) for full details.
85
-
-**oldVnode**: The previous virtual node, only available in the `update` and `componentUpdated` hooks.
78
+
ディレクティブフックには以下の引数が渡せます:
86
79
87
-
<pclass="tip">Apart from `el`, you should treat these arguments as read-only and never modify them. If you need to share information across hooks, it is recommended to do so through element's [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).</p>
80
+
-**el**: ディレクティブがひも付く要素。DOMを直接操作するために使用できます。
81
+
-**binding**: 以下のプロパティを含んでいるオブジェクト。
82
+
-**name**: `v-` 接頭辞 (prefix) 無しのディレクティブ名。
83
+
-**value**: ディレクティブに渡される値。例えば `v-my-directive="1 + 1"` では、 value は `2` です.
Vue.directive('color-swatch', function (el, binding) {
147
151
el.style.backgroundColor=binding.value
148
152
})
149
153
```
150
154
151
-
## Object Literals
155
+
## オブジェクトリテラル
152
156
153
-
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
0 commit comments