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/v2/guide/custom-directive.md
+40-37Lines changed: 40 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
-
title: Custom Directives
2
+
title: Direktif Kustom
3
3
type: guide
4
4
order: 302
5
5
---
6
6
7
-
## Intro
7
+
## Pendahuluan
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 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:
9
+
Selain kumpulan direktif standard (`v-model`dan`v-show`), Vue juga memperbolehkan Anda untuk mendaftarkan direktif kustom milik Anda. Perhatikan bahwa dalam Vue 2.0, bentuk utama dari penggunaan kembali dan abstraksi kode adalah komponen - namun mungkin ada kasus dimana Anda memerlukan akses DOM tingkat rendah pada elemen polos, dan di sini dimana direktif kustom akan tetap bermanfaat. Contoh fokus kepada elemen input, seperti satu ini:
10
10
11
11
{% raw %}
12
12
<divid="simplest-directive-example"class="demo">
@@ -24,74 +24,77 @@ new Vue({
24
24
</script>
25
25
{% endraw %}
26
26
27
-
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. Now let's build the directive that accomplishes this:
27
+
Saat halaman dimuat, elemen tersebut akan mendapatkan fokus (perhatikan: `autofocus`tidak bekerja pada Safari seluler). Faktanya, jika Anda belum mengklik hal lain sejak mengunjungi halaman ini, input di atas seharusnya sudah fokus sekarang. Sekarang mari kita bangung direktif yang mencapai hal ini:
28
28
29
29
```js
30
-
//Register a global custom directive called `v-focus`
30
+
//Mendaftarkan sebuah direktif kustom dengan nama `v-focus`
31
31
Vue.directive('focus', {
32
-
//When the bound element is inserted into the DOM...
32
+
//Saat elemen yang terikat dimasukan ke dalam DOM...
33
33
inserted:function (el) {
34
-
//Focus the element
34
+
//Fokus terhadap elemen
35
35
el.focus()
36
36
}
37
37
})
38
38
```
39
39
40
-
If you want to register a directive locally instead, components also accept a`directives` option:
40
+
Jika Anda ingin mendaftarkan direktif secara lokal, komponen juga menerima opsi`directives`:
41
41
42
42
```js
43
43
directives: {
44
44
focus: {
45
-
//directive definition
45
+
//definisi direktif
46
46
inserted:function (el) {
47
47
el.focus()
48
48
}
49
49
}
50
50
}
51
51
```
52
52
53
-
Then in a template, you can use the new `v-focus` attribute on any element, like this:
53
+
Kemudian di templat, Anda dapat menggunakan atribut `v-focus` baru pada elemen apa pun, seperti ini:
54
+
54
55
55
56
```html
56
57
<inputv-focus>
57
58
```
59
+
60
+
## Fungsi Kait
61
+
62
+
Objek definisi direktif dapat menyediakan beberapa fungsi kait (semua opsional):
58
63
59
-
## Hook Functions
60
64
61
-
A directive definition object can provide several hook functions (all optional):
65
+
-`bind`: dipanggil hanya sekali, saat direktif partama kali terikat dengan elemen. Di sini Anda dapat melakukan pekerjaan pengaturan satu kali.
62
66
63
-
-`bind`: called only once, when the directive is first bound to the element. This is where you can do one-time setup work.
67
+
-`inserted`: dipanggil ketika elemen terikat telah dimasukkan ke dalam *node* induknya (ini hanya menjamin keberadaan *node* induknya, tidak harus dalam dokumen).
64
68
65
-
-`inserted`: called when the bound element has been inserted into its parent node (this only guarantees parent node presence, not necessarily in-document).
69
+
-`update`: dipanggil ketika komponen yang memuat *VNode* telah diperbarui, __tetapi mungkin sebelum anak-anaknya diperbarui__. Nilai direktif ini mungkin berubah atau mungkin tidak berubah, tetapi Anda dapat melewati pembaruan yang tidak perlu dengan membandingkan nilai saat ini dan lama (lihat di bawah ini pada argumen hook).
66
70
67
-
-`update`: called after the containing component's VNode has updated, __but possibly before its children have updated__. The directive's value may or may not have changed, but you can skip unnecessary updates by comparing the binding's current and old values (see below on hook arguments).
71
+
<pclass="tip">Kami akan membahas VNodes secara lebih rinci [nanti](./render-function.html#The-Virtual-DOM), ketika kita membahas [fungsi render](./render-function.html).</p>
68
72
69
-
<pclass="tip">We'll cover VNodes in more detail [later](./render-function.html#The-Virtual-DOM), when we discuss [render functions](./render-function.html).</p>
73
+
-`componentUpdated`: dipanggil setelah komponen yang memuat *VNode*__dan *VNodes* anak-anaknya__ telah diperbarui.
70
74
71
-
-`componentUpdated`: called after the containing component's VNode __and the VNodes of its children__ have updated.
75
+
-`unbind`: dipanggil hanya sekali, ketika direktif terlepas dari elemennya.
72
76
73
-
-`unbind`: called only once, when the directive is unbound from the element.
77
+
Kita akan menjelajahi argumen yang dioper ke kait ini (mis. `el`,` binding`, `vnode`, dan` oldVnode`) di bagian berikutnya.
74
78
75
-
We'll explore the arguments passed into these hooks (i.e. `el`, `binding`, `vnode`, and `oldVnode`) in the next section.
76
79
77
-
## Directive Hook Arguments
80
+
## Argumen Kait Direktif
78
81
79
-
Directive hooks are passed these arguments:
82
+
Kait direktif dioper argumen ini:
80
83
81
-
-`el`: The element the directive is bound to. This can be used to directly manipulate the DOM.
82
-
-`binding`: An object containing the following properties.
83
-
-`name`: The name of the directive, without the`v-` prefix.
84
-
-`value`: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be`2`.
85
-
-`oldValue`: The previous value, only available in`update`and`componentUpdated`. It is available whether or not the value has changed.
86
-
-`expression`: The expression of the binding as a string. For example in `v-my-directive="1 + 1"`, the expression would be`"1 + 1"`.
87
-
-`arg`: The argument passed to the directive, if any. For example in `v-my-directive:foo`, the arg would be`"foo"`.
88
-
-`modifiers`: An object containing modifiers, if any. For example in `v-my-directive.foo.bar`, the modifiers object would be`{ foo: true, bar: true }`.
89
-
-`vnode`: The virtual node produced by Vue's compiler. See the [VNode API](../api/#VNode-Interface)for full details.
90
-
-`oldVnode`: The previous virtual node, only available in the`update`and`componentUpdated` hooks.
84
+
-`el`: Elemen dimana direktif terikat. Ini dapat digunakan secara langsung untuk memanipulasi DOM.
85
+
-`binding`: Objek yang berisi properti berikut.
86
+
-`name`: Nama direktif, tanpa awalan`v-`.
87
+
-`value`: Nilai yang dioper ke direktif. Misalnya pada `v-my-directive="1 + 1"`, nilainya akan menjadi`2`.
88
+
-`oldValue`: Nilai sebelumnya, hanya tersedia pada`update`dan`componentUpdated`. Tersedia apakah nilai telah berubah atau tidak.
89
+
-`expression`: Ekspresi dari *binding* sebagai *string*. Misalnya pada `v-my-directive="1 + 1"`, ekspresi akan menjadi`"1 + 1"`.
90
+
-`arg`: Argumen yang dioper ke direktif, jika ada. Misalnya pada `v-my-directive:foo`, argumen akan menjandi`"foo"`.
91
+
-`modifiers`: Objek yang memuat pengubah, jika ada. Misalnya pada `v-my-directive.foo.bar`, objek pengubah menjadi`{ foo: true, bar: true }`.
92
+
-`vnode`: *Node* virtual yang dibuat oleh penyusun Vue. Lihat [API VNode](../api/#VNode-Interface)untuk detil lebih lengkap.
93
+
-`oldVnode`: Virtual *node* sebelumnya, hanya tersedia pada kait`update`dan`componentUpdated`.
91
94
92
-
<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>
95
+
<pclass="tip">Terlepas dari `el`, Anda harus memperlakukan argumen ini sebagai hanya-baca dan tidak pernah memodifikasinya. Jika Anda perlu berbagi informasi lintas kait, direkomendasikan untuk melakukannya memlalui elemen [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset).</p>
93
96
94
-
An example of a custom directive using some of these properties:
97
+
Contoh direktif kustom menggunakan beberapa properti ini:
In many cases, you may want the same behavior on`bind`and `update`, but don't care about the other hooks. For example:
151
+
Dalam banyak kasus, Anda mungkin menginginkan perilaku yang sama pada`bind`dan`update`, tetapi tidak peduli dengan kait lainnya. Sebagai contoh:
149
152
150
153
```js
151
154
Vue.directive('color-swatch', function (el, binding) {
152
155
el.style.backgroundColor=binding.value
153
156
})
154
157
```
155
158
156
-
## Object Literals
159
+
## Objek Literal
157
160
158
-
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
161
+
Jika direktif yang Anda perlukan memerlukan beberapa nilai, Anda dapat mengirimkan objek JavaScript literal. Ingat, direktif dapat mengambil ekspresi JavaScript yang valid.
0 commit comments