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
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.
13
+
بالإضافة إلى مجموعة من الموجهات الافتراضية المتوفرة المدمجة مع الإطار (مثل`v-model`أو`v-show`) ، تسمح لك Vue أيضًا بتسجيل موجهات مخصصة خاصة بك.
14
14
15
-
We have introduced two forms of code reuse in Vue: [components](/guide/essentials/component-basics.html)and[composables](./composables). Components are the main building blocks, while composables are focused on reusing stateful logic. Custom directives, on the other hand, are mainly intended for reusing logic that involves low-level DOM access on plain elements.
15
+
لقد أدرجنا شكلين من أشكال إعادة استخدام الشيفرة في Vue: [المكونات](/guide/essentials/component-basics.html)و[composables](./composables). المكونات هي البنية الرئيسية ، في حين أن composables متميزة في إعادة استخدام منطق حالة المكون. الموجهات المخصصة ، بدورها، تهدف في الغالب إلى إعادة استخدام منطق يتضمن وصول متدني المستوى إلى الـDOM على عناصر عادية.
16
16
17
-
A custom directive is defined as an object containing lifecycle hooks similar to those of a component. The hooks receive the element the directive is bound to. Here is an example of a directive that focuses an input when the element is inserted into the DOM by Vue:
17
+
مُوجهة مخصصة تُعرَّف ككائن يحتوي على مراحل حياة مشابهة لما تحتويه المكونات. تتلقى دوال مراحل الحياة العنصر الذي عُينت الموجهة عليه. هذا مثال على موجهة تركز على إدخال عندما يُدرج العنصر في DOM بواسطة Vue:
18
18
19
19
<divclass="composition-api">
20
20
21
21
```vue
22
22
<script setup>
23
-
// enables v-focus in templates
23
+
//تمكين v-focus في القوالب
24
24
const vFocus = {
25
25
mounted: (el) => el.focus()
26
26
}
@@ -42,7 +42,7 @@ const focus = {
42
42
43
43
exportdefault {
44
44
directives: {
45
-
// enables v-focus in template
45
+
//تمكين v-focus في القوالب
46
46
focus
47
47
}
48
48
}
@@ -55,24 +55,24 @@ export default {
55
55
</div>
56
56
57
57
<divclass="demo">
58
-
<inputv-focusplaceholder="This should be focused" />
Assuming you haven't clicked elsewhere on the page, the input above should be auto-focused. This directive is more useful than the `autofocus`attribute because it works not just on page load - it also works when the element is dynamically inserted by Vue.
61
+
في حالة عدم النقر في أي مكان آخر على الصفحة ، يجب أن يكون الإدخال أعلاه مركزًا عليه تلقائيًا. هذه الموجهة أكثر فائدة من السمة `autofocus`لأنها تعمل ليس فقط عند تحميل الصفحة - بل تعمل أيضًا عند إدراج العنصر بشكل ديناميكي من قبل Vue.
62
62
63
63
<divclass="composition-api">
64
64
65
-
In`<script setup>`, any camelCase variable that starts with the `v`prefix can be used as a custom directive. In the example above, `vFocus`can be used in the template as`v-focus`.
65
+
في`<script setup>` ، يمكن استخدام أي متغير camelCase يبدأ بالبادئة `v`كموجهة مخصصة. في المثال أعلاه ، يمكن استخدام `vFocus`في القالب كـ`v-focus`.
66
66
67
-
If not using `<script setup>`, custom directives can be registered using the`directives` option:
67
+
في حالة عدم استخدام `<script setup>` ، يمكن تسجيل الموجهات المخصصة باستخدام خيار`directives`:
68
68
69
69
```js
70
70
exportdefault {
71
71
setup() {
72
72
/*...*/
73
73
},
74
74
directives: {
75
-
// enables v-focus in template
75
+
//تمكين v-focus في القوالب
76
76
focus: {
77
77
/* ... */
78
78
}
@@ -84,118 +84,117 @@ export default {
84
84
85
85
<divclass="options-api">
86
86
87
-
Similar to components, custom directives must be registered so that they can be used in templates. In the example above, we are using local registration via the `directives` option.
87
+
مثل المكونات ، يجب تسجيل الموجهات المخصصة حتى يمكن استخدامها في القوالب. في المثال أعلاه ، نستخدم التسجيل المحلي عبر خيار `directives`.
88
88
89
89
</div>
90
90
91
-
It is also common to globally register custom directives at the app level:
91
+
من الشائع أيضًا تسجيل الموجهات المخصصة على مستوى التطبيق بشكل عام:
92
92
93
93
```js
94
94
constapp=createApp({})
95
95
96
-
//make v-focus usable in all components
96
+
//جعل v-focus متاحًا في جميع المكونات
97
97
app.directive('focus', {
98
98
/* ... */
99
99
})
100
100
```
101
101
102
-
:::tip
103
-
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation. Prefer declarative templating using built-in directives such as`v-bind`when possible because they are more efficient and server-rendering friendly.
102
+
:::tip ملاحظة
103
+
يجب استخدام الموجهات المخصصة فقط عندما يمكن الوصول إلى الوظائف المطلوبة فقط عن طريق التحكم المباشر في DOM. يجب تفضيل القوالب التصريحية باستخدام الموجهات المدمجة مثل`v-bind`عند الإمكان لأنها أكثر كفاءة و ملاءمة للتصيير من جانب الخادم.
104
104
:::
105
105
106
-
## Directive Hooks {#directive-hooks}
106
+
## خطافات الموجهة {#directive-hooks}
107
107
108
-
A directive definition object can provide several hook functions (all optional):
108
+
يمكن لكائن تعريف الموجهة توفير عدة دوال خطافة (كلها اختيارية):
109
109
110
110
```js
111
111
constmyDirective= {
112
-
// called before bound element's attributes
113
-
// or event listeners are applied
112
+
//تستدعى قبل تطبيق سمات العنصر المرتبطة أو مستمعي الحدث
114
113
created(el, binding, vnode, prevVnode) {
115
-
//see below for details on arguments
114
+
//انظر أدناه للحصول على تفاصيل الوسيط
116
115
},
117
-
//called right before the element is inserted into the DOM.
116
+
//تستدعى مباشرة قبل إدراج العنصر في DOM.
118
117
beforeMount(el, binding, vnode, prevVnode) {},
119
-
// called when the bound element's parent component
120
-
// and all its children are mounted.
118
+
// تستدعى عندما يوصَّل المكون الأب وجميع أبنائه المرتبطين بالعنصر.
121
119
mounted(el, binding, vnode, prevVnode) {},
122
-
//called before the parent component is updated
120
+
//تستدعى قبل تحديث المكون الأب
123
121
beforeUpdate(el, binding, vnode, prevVnode) {},
124
-
// called after the parent component and
125
-
// all of its children have updated
122
+
// تستدعى بعد تحديث المكون الأب وجميع أبنائه
126
123
updated(el, binding, vnode, prevVnode) {},
127
-
//called before the parent component is unmounted
124
+
//تستدعى قبل فصل المكون الأب
128
125
beforeUnmount(el, binding, vnode, prevVnode) {},
129
-
//called when the parent component is unmounted
126
+
//تستدعى عند فصل المكون الأب
130
127
unmounted(el, binding, vnode, prevVnode) {}
131
128
}
132
129
```
133
130
134
-
### Hook Arguments {#hook-arguments}
131
+
### وسائط دوال الخطافات {#hook-arguments}
135
132
136
-
Directive hooks are passed these arguments:
133
+
مررت دوال الخطافات الوسائط التالية:
137
134
138
-
-`el`: the element the directive is bound to. This can be used to directly manipulate the DOM.
135
+
-`el`: العنصر المرتبط بالموجهة. يمكن استخدامه للتحكم المباشر في DOM.
139
136
140
-
-`binding`: an object containing the following properties.
137
+
-`binding`: كائن يحتوي على الخصائص التالية.
141
138
142
-
-`value`: The value passed to the directive. For example in `v-my-directive="1 + 1"`, the value would be `2`.
143
-
-`oldValue`: The previous value, only available in `beforeUpdate`and`updated`. It is available whether or not the value has changed.
144
-
-`arg`: The argument passed to the directive, if any. For example in `v-my-directive:foo`, the arg would be`"foo"`.
145
-
-`modifiers`: An object containing modifiers, if any. For example in `v-my-directive.foo.bar`, the modifiers object would be`{ foo: true, bar: true }`.
146
-
-`instance`: The instance of the component where the directive is used.
147
-
-`dir`: the directive definition object.
139
+
-`value`: القيمة الممررة إلى الموجهة. على سبيل المثال في `"v-my-directive="1 + 1`, ستكون القيمة `2`.
140
+
-`oldValue`: القيمة السابقة، متاحة فقط في `beforeUpdate`و`updated`. متاحة سواء تغيرت القيمة أم لا.
141
+
-`arg`: الوسيط الممرر إلى الموجهة، إذا كان موجودًا. على سبيل المثال في `v-my-directive:foo`, سيكون الوسيط`"foo"`.
142
+
-`modifiers`: كائن يحتوي على المعدلات، إذا كانت موجودة. على سبيل المثال في `v-my-directive.foo.bar`, سيكون كائن المعدلات`{ foo: true, bar: true }`.
143
+
-`instance`: نسخة المكون حيث تستخدم الموجهة.
144
+
-`dir`: كائن تعريف الموجهة.
148
145
149
-
-`vnode`: the underlying VNode representing the bound element.
150
-
-`prevNode`: the VNode representing the bound element from the previous render. Only available in the `beforeUpdate` and `updated` hooks.
146
+
-`vnode`: VNode الأساسي يمثل العنصر المرتبط.
151
147
152
-
As an example, consider the following directive usage:
148
+
-`prevNode`: VNode يمثل العنصر المرتبط من التصيير السابق. متاح فقط في `beforeUpdate` و `updated`.
149
+
150
+
كمثال، لنعتبر استخدام الموجهة التالي:
153
151
154
152
```vue-html
155
153
<div v-example:foo.bar="baz">
156
154
```
157
155
158
-
The `binding`argument would be an object in the shape of:
156
+
سيكون كائن الوسيط `binding`بالشكل التالي:
159
157
160
158
```js
161
159
{
162
160
arg:'foo',
163
161
modifiers: { bar:true },
164
-
value:/* value of `baz` */,
165
-
oldValue:/* value of `baz` from previous update */
162
+
value:/* قيمة `baz` */,
163
+
oldValue:/* قيمة `baz` من التحديث السابق */
164
+
166
165
}
167
166
```
168
167
169
-
Similar to built-in directives, custom directive arguments can be dynamic. For example:
168
+
مثل الموجهات المدمجة، يمكن أن تكون وسائط الموجهات المخصصة ديناميكية. على سبيل المثال:
170
169
171
170
```vue-html
172
171
<div v-example:[arg]="value"></div>
173
172
```
174
173
175
-
Here the directive argument will be reactively updated based on `arg`property in our component state.
174
+
سيُحدَّث وسيط الموجهة بشكل تفاعلي وفقًا لخاصية `arg`في حالة المكون.
176
175
177
-
:::tip Note
178
-
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).
176
+
:::tip ملاحظة
177
+
باستثناء `el`، يجب عليك التعامل مع هذه الوسائط كقراءة فقط ولا تقم بتعديلها. إذا كنت بحاجة إلى مشاركة المعلومات عبر الخطافات، فننصحك بالقيام بذلك من خلال [dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset) للعنصر.
179
178
:::
180
179
181
-
## Function Shorthand {#function-shorthand}
180
+
## اختصار الدالة {#function-shorthand}
182
181
183
-
It's common for a custom directive to have the same behavior for `mounted`and`updated`, with no need for the other hooks. In such cases we can define the directive as a function:
182
+
من الشائع أن يكون للموجهة المخصصة نفس السلوك فقط بخطافات `mounted`و`updated`، بدون حاجة للخطافات الأخرى. في هذه الحالات، يمكننا تعريف الموجهة كدالة:
184
183
185
184
```vue-html
186
185
<div v-color="color"></div>
187
186
```
188
187
189
188
```js
190
189
app.directive('color', (el, binding) => {
191
-
//this will be called for both `mounted` and `updated`
190
+
//هذا سيستدعى لكل من `mounted` و `updated`
192
191
el.style.color=binding.value
193
192
})
194
193
```
195
194
196
-
## Object Literals {#object-literals}
195
+
## الكائنات المجردة {#object-literals}
197
196
198
-
If your directive needs multiple values, you can also pass in a JavaScript object literal. Remember, directives can take any valid JavaScript expression.
197
+
إذا كانت الموجهة الخاصة بك تحتاج إلى عدة قيم، يمكنك أيضًا إرسال كائن مجرد JavaScript. تذكر أنه يمكن للموجهات أن تأخذ أي تعبير JavaScript سليم.
When used on components, custom directives will always apply to a component's root node, similar to [Fallthrough Attributes](/guide/components/attrs.html).
212
+
عند استخدامها على المكونات، ستطبق الموجهات المخصصة دائمًا على العنصر الجذري للمكون، مثل [السمات المستترة](/guide/components/attrs.html).
214
213
215
214
```vue-html
216
215
<MyComponent v-demo="test" />
217
216
```
218
217
219
218
```vue-html
220
-
<!-- template of MyComponent -->
219
+
<!-- قالب MyComponent -->
221
220
222
-
<div><!-- v-demo directive will be applied here -->
223
-
<span>My component content</span>
221
+
<div><!-- سيتم تطبيق الموجهة v-demo هنا -->
222
+
<span>محتوى المكون</span>
224
223
</div>
225
224
```
226
225
227
-
Note that components can potentially have more than one root node. When applied to a multi-root component, a directive will be ignored and a warning will be thrown. Unlike attributes, directives can't be passed to a different element with`v-bind="$attrs"`. In general, it is **not**recommended to use custom directives on components.
226
+
تجدر الملاحظة أن المكونات قد تحتوي على أكثر من عنصر جذري. عند تطبيقها على مكون متعدد الأجزاء، سيتجاهل المصرف الموجهة وسيطلق تحذير. على عكس السمات، لا يمكن تمرير الموجهات إلى عنصر مختلف باستخدام`v-bind="$attrs"`. عمومًا، **لا**ينصح باستخدام الموجهات المخصصة على المكونات.
0 commit comments