Skip to content

Commit 7d75ddc

Browse files
committed
translate custom directives
1 parent 95332c7 commit 7d75ddc

File tree

1 file changed

+60
-61
lines changed

1 file changed

+60
-61
lines changed
Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Custom Directives {#custom-directives}
1+
# المُوجِّهات المخصصة {#custom-directives}
22

33
<script setup>
44
const vFocus = {
@@ -8,19 +8,19 @@ const vFocus = {
88
}
99
</script>
1010

11-
## Introduction {#introduction}
11+
## مقدمة {#introduction}
1212

13-
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 أيضًا بتسجيل موجهات مخصصة خاصة بك.
1414

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 على عناصر عادية.
1616

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:
1818

1919
<div class="composition-api">
2020

2121
```vue
2222
<script setup>
23-
// enables v-focus in templates
23+
//تمكين v-focus في القوالب
2424
const vFocus = {
2525
mounted: (el) => el.focus()
2626
}
@@ -42,7 +42,7 @@ const focus = {
4242

4343
export default {
4444
directives: {
45-
// enables v-focus in template
45+
//تمكين v-focus في القوالب
4646
focus
4747
}
4848
}
@@ -55,24 +55,24 @@ export default {
5555
</div>
5656

5757
<div class="demo">
58-
<input v-focus placeholder="This should be focused" />
58+
<input v-focus placeholder="هذا العنصر سيركز عليه" />
5959
</div>
6060

61-
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.
6262

6363
<div class="composition-api">
6464

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`.
6666

67-
If not using `<script setup>`, custom directives can be registered using the `directives` option:
67+
في حالة عدم استخدام `<script setup>` ، يمكن تسجيل الموجهات المخصصة باستخدام خيار `directives`:
6868

6969
```js
7070
export default {
7171
setup() {
7272
/*...*/
7373
},
7474
directives: {
75-
// enables v-focus in template
75+
//تمكين v-focus في القوالب
7676
focus: {
7777
/* ... */
7878
}
@@ -84,118 +84,117 @@ export default {
8484

8585
<div class="options-api">
8686

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`.
8888

8989
</div>
9090

91-
It is also common to globally register custom directives at the app level:
91+
من الشائع أيضًا تسجيل الموجهات المخصصة على مستوى التطبيق بشكل عام:
9292

9393
```js
9494
const app = createApp({})
9595

96-
// make v-focus usable in all components
96+
// جعل v-focus متاحًا في جميع المكونات
9797
app.directive('focus', {
9898
/* ... */
9999
})
100100
```
101101

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` عند الإمكان لأنها أكثر كفاءة و ملاءمة للتصيير من جانب الخادم.
104104
:::
105105

106-
## Directive Hooks {#directive-hooks}
106+
## خطافات الموجهة {#directive-hooks}
107107

108-
A directive definition object can provide several hook functions (all optional):
108+
يمكن لكائن تعريف الموجهة توفير عدة دوال خطافة (كلها اختيارية):
109109

110110
```js
111111
const myDirective = {
112-
// called before bound element's attributes
113-
// or event listeners are applied
112+
//تستدعى قبل تطبيق سمات العنصر المرتبطة أو مستمعي الحدث
114113
created(el, binding, vnode, prevVnode) {
115-
// see below for details on arguments
114+
// انظر أدناه للحصول على تفاصيل الوسيط
116115
},
117-
// called right before the element is inserted into the DOM.
116+
// تستدعى مباشرة قبل إدراج العنصر في DOM.
118117
beforeMount(el, binding, vnode, prevVnode) {},
119-
// called when the bound element's parent component
120-
// and all its children are mounted.
118+
// تستدعى عندما يوصَّل المكون الأب وجميع أبنائه المرتبطين بالعنصر.
121119
mounted(el, binding, vnode, prevVnode) {},
122-
// called before the parent component is updated
120+
// تستدعى قبل تحديث المكون الأب
123121
beforeUpdate(el, binding, vnode, prevVnode) {},
124-
// called after the parent component and
125-
// all of its children have updated
122+
// تستدعى بعد تحديث المكون الأب وجميع أبنائه
126123
updated(el, binding, vnode, prevVnode) {},
127-
// called before the parent component is unmounted
124+
// تستدعى قبل فصل المكون الأب
128125
beforeUnmount(el, binding, vnode, prevVnode) {},
129-
// called when the parent component is unmounted
126+
// تستدعى عند فصل المكون الأب
130127
unmounted(el, binding, vnode, prevVnode) {}
131128
}
132129
```
133130

134-
### Hook Arguments {#hook-arguments}
131+
### وسائط دوال الخطافات {#hook-arguments}
135132

136-
Directive hooks are passed these arguments:
133+
مررت دوال الخطافات الوسائط التالية:
137134

138-
- `el`: the element the directive is bound to. This can be used to directly manipulate the DOM.
135+
- `el`: العنصر المرتبط بالموجهة. يمكن استخدامه للتحكم المباشر في DOM.
139136

140-
- `binding`: an object containing the following properties.
137+
- `binding`: كائن يحتوي على الخصائص التالية.
141138

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`: كائن تعريف الموجهة.
148145

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 الأساسي يمثل العنصر المرتبط.
151147

152-
As an example, consider the following directive usage:
148+
- `prevNode`: VNode يمثل العنصر المرتبط من التصيير السابق. متاح فقط في `beforeUpdate` و `updated`.
149+
150+
كمثال، لنعتبر استخدام الموجهة التالي:
153151

154152
```vue-html
155153
<div v-example:foo.bar="baz">
156154
```
157155

158-
The `binding` argument would be an object in the shape of:
156+
سيكون كائن الوسيط `binding` بالشكل التالي:
159157

160158
```js
161159
{
162160
arg: 'foo',
163161
modifiers: { bar: true },
164-
value: /* value of `baz` */,
165-
oldValue: /* value of `baz` from previous update */
162+
value: /* قيمة `baz` */,
163+
oldValue: /* قيمة `baz` من التحديث السابق */
164+
166165
}
167166
```
168167

169-
Similar to built-in directives, custom directive arguments can be dynamic. For example:
168+
مثل الموجهات المدمجة، يمكن أن تكون وسائط الموجهات المخصصة ديناميكية. على سبيل المثال:
170169

171170
```vue-html
172171
<div v-example:[arg]="value"></div>
173172
```
174173

175-
Here the directive argument will be reactively updated based on `arg` property in our component state.
174+
سيُحدَّث وسيط الموجهة بشكل تفاعلي وفقًا لخاصية `arg` في حالة المكون.
176175

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) للعنصر.
179178
:::
180179

181-
## Function Shorthand {#function-shorthand}
180+
## اختصار الدالة {#function-shorthand}
182181

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`، بدون حاجة للخطافات الأخرى. في هذه الحالات، يمكننا تعريف الموجهة كدالة:
184183

185184
```vue-html
186185
<div v-color="color"></div>
187186
```
188187

189188
```js
190189
app.directive('color', (el, binding) => {
191-
// this will be called for both `mounted` and `updated`
190+
// هذا سيستدعى لكل من `mounted` و `updated`
192191
el.style.color = binding.value
193192
})
194193
```
195194

196-
## Object Literals {#object-literals}
195+
## الكائنات المجردة {#object-literals}
197196

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 سليم.
199198

200199
```vue-html
201200
<div v-demo="{ color: 'white', text: 'hello!' }"></div>
@@ -208,20 +207,20 @@ app.directive('demo', (el, binding) => {
208207
})
209208
```
210209

211-
## Usage on Components {#usage-on-components}
210+
## الاستخدام على المكونات {#usage-on-components}
212211

213-
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).
214213

215214
```vue-html
216215
<MyComponent v-demo="test" />
217216
```
218217

219218
```vue-html
220-
<!-- template of MyComponent -->
219+
<!-- قالب MyComponent -->
221220
222-
<div> <!-- v-demo directive will be applied here -->
223-
<span>My component content</span>
221+
<div><!-- سيتم تطبيق الموجهة v-demo هنا -->
222+
<span>محتوى المكون</span>
224223
</div>
225224
```
226225

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

Comments
 (0)