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/guide/essentials/reactivity-fundamentals.md
+28-26
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@ outline: deep
4
4
5
5
# Nguyên tắc cơ bản về Reactivity
6
6
7
-
:::tip API Preference
8
-
This page and many other chapters later in the guide contain different content for Options API and Composition API. Your current preference is <spanclass="options-api">Options API</span><spanclass="composition-api">Composition API</span>. You can toggle between the API styles using the "API Preference" switches at the top of the left sidebar.
7
+
:::tip Tùy chọn API
8
+
Kể từ trang này, mục chỉ dẫn sẽ có các nội dung khác nhau về Options API và Composition API. Tùy chọn hiện tại của bạn là <spanclass="options-api">Options API</span><spanclass="composition-api">Composition API</span>. Bạn có thể thay đổi qua lại giữa các hình thức API bằng cách sử dụng công tắc chuyển đổi "API Preference" tại đầu cột bên trái.
9
9
:::
10
10
11
-
## Declaring Reactive State
11
+
## Khai báo Reactive State
12
12
13
13
<divclass="options-api">
14
14
15
-
With Options API, we use the `data`option to declare reactive state of a component. The option value should be a function that returns an object. Vue will call the function when creating a new component instance, and wrap the returned object in its reactivity system. Any top-level properties of this object are proxied on the component instance (`this` in methods and lifecycle hooks):
15
+
Với tùy chọn Options API, chúng ta sử dụng thuộc tính `data`để khai báo state của component. Giá trị của thuộc tính `data` này nên là một hàm trả về một object. Vue sẽ thực thi hàm này mỗi khi tạo mới một component instance, và nhúng object của hàm trả về vào hệ thống reactivity của nó. Bất cứ thuộc tính thượng cấp (top-level) nào của đối tượng đều được ủy quyền trên component instance (đối tượng this trong phương thức (methods) và các hook vòng đời (lifecycle)):
16
16
17
17
```js{2-6}
18
18
export default {
@@ -22,28 +22,28 @@ export default {
22
22
}
23
23
},
24
24
25
-
// `mounted` is a lifecycle hook which we will explain later
25
+
// `mounted` là một hook vòng đời (lifecycle) mà chúng ta sẽ học ở các phần sau
26
26
mounted() {
27
-
// `this` refers to the component instance.
27
+
// `this` tham chiếu tới component instance
28
28
console.log(this.count) // => 1
29
29
30
-
// data can be mutated as well
30
+
// data cũng có thể được thay đổi (mutate)
31
31
this.count = 2
32
32
}
33
33
}
34
34
```
35
35
36
-
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgZGF0YSgpIHtcbiAgICByZXR1cm4ge1xuICAgICAgY291bnQ6IDFcbiAgICB9XG4gIH0sXG5cbiAgLy8gYG1vdW50ZWRgIGlzIGEgbGlmZWN5Y2xlIGhvb2sgd2hpY2ggd2Ugd2lsbCBleHBsYWluIGxhdGVyXG4gIG1vdW50ZWQoKSB7XG4gICAgLy8gYHRoaXNgIHJlZmVycyB0byB0aGUgY29tcG9uZW50IGluc3RhbmNlLlxuICAgIGNvbnNvbGUubG9nKHRoaXMuY291bnQpIC8vID0+IDFcblxuICAgIC8vIGRhdGEgY2FuIGJlIG11dGF0ZWQgYXMgd2VsbFxuICAgIHRoaXMuY291bnQgPSAyXG4gIH1cbn1cbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIENvdW50IGlzOiB7eyBjb3VudCB9fVxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59In0=)
36
+
[Thử ngay trong Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgZGF0YSgpIHtcbiAgICByZXR1cm4ge1xuICAgICAgY291bnQ6IDFcbiAgICB9XG4gIH0sXG5cbiAgLy8gYG1vdW50ZWRgIGlzIGEgbGlmZWN5Y2xlIGhvb2sgd2hpY2ggd2Ugd2lsbCBleHBsYWluIGxhdGVyXG4gIG1vdW50ZWQoKSB7XG4gICAgLy8gYHRoaXNgIHJlZmVycyB0byB0aGUgY29tcG9uZW50IGluc3RhbmNlLlxuICAgIGNvbnNvbGUubG9nKHRoaXMuY291bnQpIC8vID0+IDFcblxuICAgIC8vIGRhdGEgY2FuIGJlIG11dGF0ZWQgYXMgd2VsbFxuICAgIHRoaXMuY291bnQgPSAyXG4gIH1cbn1cbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIENvdW50IGlzOiB7eyBjb3VudCB9fVxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59In0=)
37
37
38
-
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined`or some other placeholder value for properties where the desired value isn't yet available.
38
+
Các thuộc tính instance này chỉ được thêm vào khi instance đã được tạo lần đầu, nên bạn cần phải chắc chắn rằng chúng phải có sẵn trong object trả về của hàm `data`. Khi cần thiết, có thể sử dụng `null`, `undefined`hoặc một vài giá trị mẫu cho các thuộc tính chưa thực sự khả dụng.
39
39
40
-
It is possible to add a new property directly to`this`without including it in `data`. However, properties added this way will not be able to trigger reactive updates.
40
+
Bạn có thể thêm trực tiếp thuộc tính mới vào`this`mà không cần thông qua `data`. Tuy nhiên các thuộc tính được thêm kiểu này sẽ không có tính reactive.
41
41
42
-
Vue uses a `$`prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_`for internal properties. You should avoid using names for top-level`data`properties that start with either of these characters.
42
+
Vue sử dụng tiền tố `$`cho các API tích hợp khi hiển thị nó qua component instance. Nó cũng sử dụng tiền tố `_`cho các thuộc tính bên trong (internal). Bạn nên tránh đặt tên cho các state thượng cấp (top-level) trong `data`mà bắt đầu với các ký tự trên.
43
43
44
-
### Reactive Proxy vs. Original\*
44
+
### Reactive Proxy vs. Bản gốc\*
45
45
46
-
In Vue 3, data is made reactive by leveraging [JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). Users coming from Vue 2 should be aware of the following edge case:
46
+
Ở Vue 3, dữ liệu được reactive bằng cách tận dụng tính năng [JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). Người dùng Vue 2 nên lưu ý edge case sau:
47
47
48
48
```js
49
49
exportdefault {
@@ -61,35 +61,35 @@ export default {
61
61
}
62
62
```
63
63
64
-
When you access `this.someObject`after assigning it, the value is a reactive proxy of the original `newObject`. **Unlike in Vue 2, the original `newObject`is left intact and will not be made reactive: make sure to always access reactive state as a property of`this`.**
64
+
Khi bạn truy cập vào `this.someObject`sau khi đã gán giá trị cho nó, giá trị lúc này đã trở thành một reactive proxy của `newObject` ban đầu. **Không giống như Vue 2, biến `newObject`ban đầu sẽ được giữ nguyên và không trở nên reactive: hãy chắc chắn rằng bạn luôn truy cập vào các reactive state như là một thuộc tính của`this`.**
65
65
66
66
</div>
67
67
68
68
<divclass="composition-api">
69
69
70
-
We can create a reactive object or array with the[`reactive()`](/api/reactivity-core.html#reactive) function:
70
+
Chúng ta có thể khởi tạo một reactive object hoặc array với hàm[`reactive()`](/api/reactivity-core.html#reactive):
71
71
72
72
```js
73
73
import { reactive } from'vue'
74
74
75
75
conststate=reactive({ count:0 })
76
76
```
77
77
78
-
Reactive objects are[JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)and behave just like normal objects. The difference is that Vue is able to track the property access and mutations of a reactive object. If you are curious about the details, we explain how Vue's reactivity system works in [Reactivity in Depth](/guide/extras/reactivity-in-depth.html) - but we recommend reading it after you have finished the main guide.
78
+
Những Reactive object là[JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)và hoạt động cũng giống như object bình thường. Sự khác biệt duy nhất là Vue có thể theo dõi được lượt truy cập thuộc tính và sự thay đổi (mutation) của một reactive object. Nếu bạn có hứng thú về các chi tiết sâu hơn, chúng tôi có giải thích cách mà hệ thống Reactivity của Vue hoạt động trong [Sâu hơn về Reactivity](/guide/extras/reactivity-in-depth.html) - nhưng chúng tôi khuyến nghị bạn chỉ nên đọc chúng sau khi hoàn thành hết mục chỉ dẫn chính.
79
79
80
-
See also: [Typing Reactive](/guide/typescript/composition-api.html#typing-reactive) <supclass="vt-badge ts" />
To use reactive state in a component's template, declare and return them from a component's `setup()`function:
82
+
Để sử dụng reactive state trong template của component, khai báo và trả về chúng từ hàm `setup()`của component:
83
83
84
84
```js{5,9-11}
85
85
import { reactive } from 'vue'
86
86
87
87
export default {
88
-
// `setup` is a special hook dedicated for composition API.
88
+
// `setup` là một hook đặc biệt dành riêng cho Composition API
89
89
setup() {
90
90
const state = reactive({ count: 0 })
91
91
92
-
// expose the state to the template
92
+
// hiển thị state lên template
93
93
return {
94
94
state
95
95
}
@@ -101,7 +101,7 @@ export default {
101
101
<div>{{ state.count }}</div>
102
102
```
103
103
104
-
Similarly, we can declare functions that mutate reactive state in the same scope, and expose it as a method alongside the state:
104
+
Tương tự thế, chúng ta có thể khai báo những hàm mà thay đổi (mutate) reactive state trong cùng một phạm vi (scope), và trả chúng ra như là một hàm bên cạnh các state:
105
105
106
106
```js{7-9,14}
107
107
import { reactive } from 'vue'
@@ -114,7 +114,7 @@ export default {
114
114
state.count++
115
115
}
116
116
117
-
// don't forget to expose the function as well.
117
+
// đừng quên hiển thị các hàm ra nhé
118
118
return {
119
119
state,
120
120
increment
@@ -123,7 +123,7 @@ export default {
123
123
}
124
124
```
125
125
126
-
Exposed methods are typically used as event listeners:
126
+
Các hàm đã được hiển thị thường được sử dụng như là các bộ lắng nghe sự kiện (event listener):
127
127
128
128
```vue-html
129
129
<button @click="increment">
@@ -135,6 +135,8 @@ Exposed methods are typically used as event listeners:
135
135
136
136
Manually exposing state and methods via `setup()` can be verbose. Luckily, it is only necessary when not using a build step. When using Single-File Components (SFCs), we can greatly simplify the usage with `<script setup>`:
137
137
138
+
Trả về thủ công các state và phương thức (methods) thông qua `setup()` có thể hơi dài dòng. May mắn thay, chúng chỉ cần thiết khi bạn không sử dụng các công cụ build. Khi sử dụng với Single-File Components (SFCs), bạn có thể tối giản chúng bằng `<script setup>`:
139
+
138
140
```vue
139
141
<script setup>
140
142
import { reactive } from 'vue'
@@ -153,11 +155,11 @@ function increment() {
153
155
</template>
154
156
```
155
157
156
-
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlYWN0aXZlIH0gZnJvbSAndnVlJ1xuXG5jb25zdCBzdGF0ZSA9IHJlYWN0aXZlKHsgY291bnQ6IDAgfSlcblxuZnVuY3Rpb24gaW5jcmVtZW50KCkge1xuICBzdGF0ZS5jb3VudCsrXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8YnV0dG9uIEBjbGljaz1cImluY3JlbWVudFwiPlxuICAgIHt7IHN0YXRlLmNvdW50IH19XG4gIDwvYnV0dG9uPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59In0=)
158
+
[Thử ngay trong Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlYWN0aXZlIH0gZnJvbSAndnVlJ1xuXG5jb25zdCBzdGF0ZSA9IHJlYWN0aXZlKHsgY291bnQ6IDAgfSlcblxuZnVuY3Rpb24gaW5jcmVtZW50KCkge1xuICBzdGF0ZS5jb3VudCsrXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8YnV0dG9uIEBjbGljaz1cImluY3JlbWVudFwiPlxuICAgIHt7IHN0YXRlLmNvdW50IH19XG4gIDwvYnV0dG9uPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59In0=)
157
159
158
-
Top-level imports and variables declared in `<script setup>`are automatically usable in the template of the same component.
160
+
Các import và các biến thượng cấp mà đã được khai báo trong `<script setup>`đều tự động khả dụng trong template của component đó.
159
161
160
-
> For the rest of the guide, we will be primarily using SFC +`<script setup>`syntax for Composition API code examples, as that is the most common usage for Vue developers.
162
+
> Kể từ giờ, mục chỉ dẫn sẽ sử dụng SFC và cú pháp`<script setup>`cho các đoạn mã ví dụ của Composition API, vì chúng là cách sử dụng phổ biến nhất với các người dùng Vue.
0 commit comments