|
2 | 2 | import SwitchComponent from './keep-alive-demos/SwitchComponent.vue'
|
3 | 3 | </script>
|
4 | 4 |
|
5 |
| -# KeepAlive {#keepalive} |
| 5 | +# مكون KeepAlive {#keepalive} |
6 | 6 |
|
7 |
| -`<KeepAlive>` is a built-in component that allows us to conditionally cache component instances when dynamically switching between multiple components. |
| 7 | +`<KeepAlive>` هو مكون مدمج يسمح لنا بتخزين مؤقت لنسخ المكونات عند التبديل بين مكونات متعددة بشكل ديناميكي. |
8 | 8 |
|
9 |
| -## Basic Usage {#basic-usage} |
| 9 | +## استعمال أساسي {#basic-usage} |
10 | 10 |
|
11 |
| -In the Component Basics chapter, we introduced the syntax for [Dynamic Components](/guide/essentials/component-basics.html#dynamic-components), using the `<component>` special element: |
| 11 | +في فصل أساسيات المكونات ، أدرجنا صيغة [المكونات الديناميكية](/guide/essentials/component-basics.html#dynamic-components) باستخدام عنصر `<component>` الخاص: |
12 | 12 |
|
13 | 13 | ```vue-html
|
14 | 14 | <component :is="activeComponent" />
|
15 | 15 | ```
|
16 | 16 |
|
17 |
| -By default, an active component instance will be unmounted when switching away from it. This will cause any changed state it holds to be lost. When this component is displayed again, a new instance will be created with only the initial state. |
| 17 | +افتراضيا، ستُفصل نسخة المكون النشط عند التبديل عنه. سيؤدي ذلك إلى فقدان أي حالة غُيِّرت عند عرض هذا المكون مرة أخرى، ستنشأ نسخة جديدة من المكون مع حالته الأولية فقط. |
18 | 18 |
|
19 |
| -In the example below, we have two stateful components - A contains a counter, while B contains a message synced with an input via `v-model`. Try updating the state of one of them, switch away, and then switch back to it: |
| 19 | +في المثال أدناه، لدينا مكونين ذوي حالة - يحتوي المكون A على عداد، بينما يحتوي B على رسالة متزامنة مع إدخال عبر `v-model`. حاول تحديث حالة أحدهما، والتبديل عنه، ثم الرجوع مرة أخرى إليه: |
20 | 20 |
|
21 | 21 | <SwitchComponent />
|
22 | 22 |
|
23 |
| -You'll notice that when switched back, the previous changed state would have been reset. |
| 23 | +ستلاحظ أنه عند الرجوع إليه، سيُعاد تعيين الحالة السابقة التي غُيِّرت. |
24 | 24 |
|
25 |
| -Creating fresh component instance on switch is normally useful behavior, but in this case, we'd really like the two component instances to be preserved even when they are inactive. To solve this problem, we can wrap our dynamic component with the `<KeepAlive>` built-in component: |
| 25 | +إن إنشاء نسخة جديدة من المكون عند التبديل هو سلوك مفيد عادة، لكن في هذه الحالة، نود حقا أن يحتفظ بنسختين من المكون حتى عندما تكونا غير نشطتين. لحل هذه المشكلة، يمكننا تغليف المكون الديناميكي بمكون `<KeepAlive>` المدمج: |
26 | 26 |
|
27 | 27 | ```vue-html
|
28 |
| -<!-- Inactive components will be cached! --> |
| 28 | +<!-- المكونات غير النشطة ستُخزن مؤقتا! --> |
29 | 29 | <KeepAlive>
|
30 | 30 | <component :is="activeComponent" />
|
31 | 31 | </KeepAlive>
|
32 | 32 | ```
|
33 | 33 |
|
34 |
| -Now, the state will be persisted across component switches: |
| 34 | +الآن، سيحتفظ بالحالة عبر التبديل بين المكونات: |
35 | 35 |
|
36 | 36 | <SwitchComponent use-KeepAlive />
|
37 | 37 |
|
38 | 38 | <div class="composition-api">
|
39 | 39 |
|
40 |
| -[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHNoYWxsb3dSZWYgfSBmcm9tICd2dWUnXG5pbXBvcnQgQ29tcEEgZnJvbSAnLi9Db21wQS52dWUnXG5pbXBvcnQgQ29tcEIgZnJvbSAnLi9Db21wQi52dWUnXG5cbmNvbnN0IGN1cnJlbnQgPSBzaGFsbG93UmVmKENvbXBBKVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPGRpdiBjbGFzcz1cImRlbW9cIj5cbiAgICA8bGFiZWw+PGlucHV0IHR5cGU9XCJyYWRpb1wiIHYtbW9kZWw9XCJjdXJyZW50XCIgOnZhbHVlPVwiQ29tcEFcIiAvPiBBPC9sYWJlbD5cbiAgICA8bGFiZW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gbXNnIH19PC9zcGFuPlxuICA8aW5wdXQgdi1tb2RlbD1cIm1zZ1wiPlxuPC90ZW1wbGF0ZT5cbiJ9) |
| 45 | +[اختبرها في حقل التجارب](https://sfc.vuejs.org/#eNqtVEtu2zAQvcqAG7ewLQZdCqpRu8teQRtaoh2m4gck5SYwvOoHRS8SdFEERVe9iXybDklbrdwggZEAhqUZvnkk37zRlsyNyTYtJzkpXGWF8bNSCWm09fBWSzOHldUSRhmNUYCOBoDFALA4AABKxa8jqOYr1jYetiFbIUYrrrzLYZs2mBxodpMAqJlnL14mMIDlvrXqGGF5ay0W5zCKpXEjgF144B/+CtpfAgPPpWmY5xgBFLXYQNUw516XpOZSlyTmcaVhS97MCqFM68HfGI4Iy2qBENhMpa55g5nD5iHHmjZg4iEwpjOYFzSxPAPlIlEuTijfcW7mjdik68RULyfkIlyr55sVtF871tMhQUFREHwr6D8ykQlJvZ1KZrIrpxUaI8pfHhZcSbB1ibIk2O0Ql+TSe+NySt2qCha4cpm2a4pvmW2VF5Jn3Mnp0uoPjlskLkls94GDYnLD7RQPX3PL7UOcJ9D/eI9mwKv0lh24+z5fPmI7jXfI4eIsu5lZd7v/tP+8/7j/uv8CIeh+xNS3PNjFJJgzTIHzN03ovmR2LdTUivUl7vfqwlxjK2Pl7+6uu+3uUPptOg7sdrg7FieaZeu9VvCmakT1PjghYMZjLB8XNC3e0+t+ZJ8okHRrnMlHxvE8hdD95yn0s/sVn9+jRniiE4XSKP6dPUTEL8BQk90fzkvNng==) |
46 | 46 |
|
47 | 47 | </div>
|
48 | 48 |
|
49 |
| -:::tip |
50 |
| -When used in [DOM templates](/guide/essentials/component-basics.html#dom-template-parsing-caveats), it should be referenced as `<keep-alive>`. |
| 49 | +:::tip ملاحظة |
| 50 | +عند استخدامها في [قوالب الـDOM](/guide/essentials/component-basics.html#dom-template-parsing-caveats)، يجب إشارة إليها باستخدام `<keep-alive>`. |
51 | 51 | :::
|
52 | 52 |
|
53 |
| -## Include / Exclude {#include-exclude} |
| 53 | +## التضمين / استبعاد {#include-exclude} |
54 | 54 |
|
55 |
| -By default, `<KeepAlive>` will cache any component instance inside. We can customize this behavior via the `include` and `exclude` props. Both props can be a comma-delimited string, a `RegExp`, or an array containing either types: |
| 55 | +افتراضيا، ستخزن أي نسخة للمكون داخل `<KeepAlive>`، ويمكننا تخصيص هذا السلوك عبر التوابع `include` و `exclude`. يمكن أن تكون كلتا الخاصيتين عبارة عن سلسلة نصية مفصولة بفواصل، تعبير نمطي `RegExp`، أو مصفوفة تحتوي كلا النوعين: |
56 | 56 |
|
57 | 57 | ```vue-html
|
58 |
| -<!-- comma-delimited string --> |
| 58 | +<!-- سلسلة نصية مفصولة بفواصل --> |
59 | 59 | <KeepAlive include="a,b">
|
60 | 60 | <component :is="view" />
|
61 | 61 | </KeepAlive>
|
62 | 62 |
|
63 |
| -<!-- regex (use `v-bind`) --> |
| 63 | +<!-- تعبير نمطي (استخدم `v-bind`) --> |
64 | 64 | <KeepAlive :include="/a|b/">
|
65 | 65 | <component :is="view" />
|
66 | 66 | </KeepAlive>
|
67 | 67 |
|
68 |
| -<!-- Array (use `v-bind`) --> |
| 68 | +<!-- مصفوفة (استخدم `v-bind`) --> |
69 | 69 | <KeepAlive :include="['a', 'b']">
|
70 | 70 | <component :is="view" />
|
71 | 71 | </KeepAlive>
|
72 | 72 | ```
|
73 | 73 |
|
74 |
| -The match is checked against the component's [`name`](/api/options-misc.html#name) option, so components that need to be conditionally cached by `KeepAlive` must explicitly declare a `name` option. |
| 74 | +يُتحقق من التطابق عبر خيار [`name`](/api/options-misc.html#name) للمكون، لذا يجب على المكونات التي تحتاج إلى التخزين الشرطي عبر `KeepAlive` التعريف بخيار `name` بشكل صريح. |
75 | 75 |
|
76 |
| -:::tip |
77 |
| -Since version 3.2.34, a single-file component using `<script setup>` will automatically infer its `name` option based on the filename, removing the need to manually declare the name. |
| 76 | +:::tip ملاحظة |
| 77 | +من الإصدار 3.2.34، سيتم تلقائيا تحديد خيار `name` للمكون المستخدم في `<script setup>` بناءً على اسم الملف، مما يزيل الحاجة إلى تعريف الاسم يدويا. |
78 | 78 | :::
|
79 | 79 |
|
80 |
| -## Max Cached Instances {#max-cached-instances} |
| 80 | +## الحد الأقصى للنسخ المخزنة {#max-cached-instances} |
81 | 81 |
|
82 |
| -We can limit the maximum number of component instances that can be cached via the `max` prop. When `max` is specified, `<KeepAlive>` behaves like an [LRU cache](<https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)>): if the number of cached instances is about to exceed the specified max count, the least recently accessed cached instance will be destroyed to make room for the new one. |
| 82 | +يمكننا تحديد الحد الأقصى لعدد النسخ المخزنة عبر خاصية `max`. عند تحديد `max`، ستتصرف `<KeepAlive>` كـ[ذاكرة تخزين مؤقت](<https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)>): إذا كان عدد النسخ المخزنة قريبًا من الوصول إلى الحد الأقصى المحدد، ستُفصل النسخة الأقدم المخزنة لإنشاء مساحة للنسخة الجديدة. |
83 | 83 |
|
84 | 84 | ```vue-html
|
85 | 85 | <KeepAlive :max="10">
|
86 | 86 | <component :is="activeComponent" />
|
87 | 87 | </KeepAlive>
|
88 | 88 | ```
|
89 | 89 |
|
90 |
| -## Lifecycle of Cached Instance {#lifecycle-of-cached-instance} |
| 90 | +## دورة حياة النسخة المخزنة {#lifecycle-of-cached-instance} |
91 | 91 |
|
92 |
| -When a component instance is removed from the DOM but is part of a component tree cached by `<KeepAlive>`, it goes into a **deactivated** state instead of being unmounted. When a component instance is inserted into the DOM as part of a cached tree, it is **activated**. |
| 92 | +عندما تُزال نسخة للمكون من الـDOM ولكنها عبارة عن جزء من شجرة مكونات مخزنة بواسطة `<KeepAlive>`، ستتحول إلى حالة **خاملة** بدلاً من إزالتها من الـDOM. عندما تُضاف نسخة للمكون إلى الـDOM كجزء من شجرة مكونات مخزنة، ستتحول إلى حالة **مفعلة**. |
93 | 93 |
|
94 | 94 | <div class="composition-api">
|
95 | 95 |
|
96 |
| -A kept-alive component can register lifecycle hooks for these two states using [`onActivated()`](/api/composition-api-lifecycle.html#onactivated) and [`onDeactivated()`](/api/composition-api-lifecycle.html#ondeactivated): |
| 96 | +مكون مخزن يمكنه تسجيل دوال دورة حياة لهذين الحالتين باستخدام [`()onActivated`](/api/composition-api-lifecycle.html#onactivated) و[`()onDeactivated`](/api/composition-api-lifecycle.html#ondeactivated): |
97 | 97 |
|
98 | 98 | ```vue
|
99 | 99 | <script setup>
|
100 | 100 | import { onActivated, onDeactivated } from 'vue'
|
101 | 101 |
|
102 | 102 | onActivated(() => {
|
103 |
| - // called on initial mount |
104 |
| - // and every time it is re-inserted from the cache |
| 103 | + // يستدعى عند الوصل |
| 104 | + // وكل مرة يتم إدراجه من الذاكرة المؤقتة |
105 | 105 | })
|
106 | 106 |
|
107 | 107 | onDeactivated(() => {
|
108 |
| - // called when removed from the DOM into the cache |
109 |
| - // and also when unmounted |
| 108 | + // يستدعى عند إزالته من الـDOM إلى الذاكرة المؤقتة |
| 109 | + // وكذلك عند إزالته |
110 | 110 | })
|
111 | 111 | </script>
|
112 | 112 | ```
|
113 | 113 |
|
114 | 114 | </div>
|
115 | 115 | <div class="options-api">
|
116 | 116 |
|
117 |
| -A kept-alive component can register lifecycle hooks for these two states using [`activated`](/api/options-lifecycle.html#activated) and [`deactivated`](/api/options-lifecycle.html#deactivated) hooks: |
| 117 | +مكون مخزن يمكنه تسجيل دوال دورة حياة لهذين الحالتين باستخدام دوال [`activated`](/api/options-lifecycle.html#activated) و[`deactivated`](/api/options-lifecycle.html#deactivated): |
118 | 118 |
|
119 | 119 | ```js
|
120 | 120 | export default {
|
121 | 121 | activated() {
|
122 |
| - // called on initial mount |
123 |
| - // and every time it is re-inserted from the cache |
| 122 | + // يستدعى عند الوصل |
| 123 | + // وكل مرة يتم إدراجه من الذاكرة المؤقتة |
124 | 124 | },
|
125 | 125 | deactivated() {
|
126 |
| - // called when removed from the DOM into the cache |
127 |
| - // and also when unmounted |
| 126 | + // يستدعى عند إزالته من الـDOM إلى الذاكرة المؤقتة |
| 127 | + // وكذلك عند إزالته |
128 | 128 | }
|
129 | 129 | }
|
130 | 130 | ```
|
131 | 131 |
|
132 | 132 | </div>
|
133 | 133 |
|
134 |
| -Note that: |
| 134 | +تجدر الإشارة إلى: |
135 | 135 |
|
136 |
| -- <span class="composition-api">`onActivated`</span><span class="options-api">`activated`</span> is also called on mount, and <span class="composition-api">`onDeactivated`</span><span class="options-api">`deactivated`</span> on unmount. |
| 136 | +- <span class="composition-api">`onActivated`</span><span class="options-api">`activated`</span> يستدعى أيضًا عند الوصل، و<span class="composition-api">`onDeactivated`</span><span class="options-api">`deactivated`</span> عند الفصل. |
137 | 137 |
|
138 |
| -- Both hooks work for not only the root component cached by `<KeepAlive>`, but also descendant components in the cached tree. |
| 138 | +- تعمل كل من الدوال على جذر المكون المخزن بواسطة `<KeepAlive>`، ولكنها تعمل أيضًا على المكونات الفرعية في شجرة المكونات المخزنة. |
139 | 139 |
|
140 | 140 | ---
|
141 | 141 |
|
142 |
| -**Related** |
| 142 | +**ذات علاقة** |
143 | 143 |
|
144 |
| -- [`<KeepAlive>` API reference](/api/built-in-components.html#keepalive) |
| 144 | +- [ مرجع الواجهة البرمجية لـ`<KeepAlive>`](/api/built-in-components.html#keepalive) |
0 commit comments