diff --git a/src/guide/essentials/lifecycle.md b/src/guide/essentials/lifecycle.md index 7bbf5c2c5a..5b00b6e4e9 100644 --- a/src/guide/essentials/lifecycle.md +++ b/src/guide/essentials/lifecycle.md @@ -1,10 +1,10 @@ # Lifecycle Hooks -Each Vue component instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages. +Mỗi Vue component instance khi được tạo ra đều trải qua một loạt các bước khởi tạo, ví dụ như là nó cần thiết lập quan sát dữ liệu (data observation), biên dịch template, mount instance đó ra DOM, và cập nhật DOM mỗi khi dữ liệu thay đổi. Đồng thời, nó cũng chạy các hàm được gọi là lifecycle hook để chúng ta có cơ hội chạy code của mình ở một số giai đoạn nhất định. -## Registering Lifecycle Hooks +## Đăng ký Lifecycle Hooks -For example, the `onMounted``mounted` hook can be used to run code after the component has finished the initial rendering and created the DOM nodes: +VD, `onMounted``mounted` hook có thể sử dụng để chạy code sau khi component đã kết thúc render lần đầu và tạo các DOM node:
@@ -31,17 +31,16 @@ export default {
-There are also other hooks which will be called at different stages of the instance's lifecycle, with the most commonly used being [`onMounted`](/api/composition-api-lifecycle.html#onmounted), [`onUpdated`](/api/composition-api-lifecycle.html#onupdated), and [`onUnmounted`](/api/composition-api-lifecycle.html#onunmounted).[`mounted`](/api/options-lifecycle.html#mounted), [`updated`](/api/options-lifecycle.html#updated), and [`unmounted`](/api/options-lifecycle.html#unmounted). +Ngoài ra còn có các hook khác được gọi ở các giai đoạn khác nhau trong vòng đời của instance, trong đó thường được sử dụng nhất đó là [`onMounted`](/api/composition-api-lifecycle.html#onmounted), [`onUpdated`](/api/composition-api-lifecycle.html#onupdated), and [`onUnmounted`](/api/composition-api-lifecycle.html#onunmounted).[`mounted`](/api/options-lifecycle.html#mounted), [`updated`](/api/options-lifecycle.html#updated), và [`unmounted`](/api/options-lifecycle.html#unmounted).
- -All lifecycle hooks are called with their `this` context pointing to the current active instance invoking it. Note this means you should avoid using arrow functions when declaring lifecycle hooks, as you won't be able to access the component instance via `this` if you do so. +Tất cả các lifecycle hook được gọi với `this` context sẽ trỏ đến instance hiện tại đang được gọi. Lưu ý rằng điều này có nghĩa là bạn nên tránh sử dụng arrow functions khi khai báo các lifecycle hook, vì như thế nó sẽ làm bạn không thể truy cập đến component instance qua `this` nếu như bạn làm như trên.
-When calling `onMounted`, Vue automatically associates the registered callback function with the current active component instance. This requires these hooks to be registered **synchronously** during component setup. For example, do not do this: +Khi gọi `onMounted`, Vue tự động liên kết callback function với component instance hiện tại. Đều này yêu cầu các hook này phải được đăng ký **đồng bộ** trong khi thiết lập component. VD, chúng ta không nên làm như thế này: ```js setTimeout(() => { @@ -51,16 +50,16 @@ setTimeout(() => { }, 100) ``` -Do note this doesn't mean that the call must be placed lexically inside `setup()` or `