diff --git a/src/guide/essentials/template-syntax.md b/src/guide/essentials/template-syntax.md index 8a42bff..ca6842e 100644 --- a/src/guide/essentials/template-syntax.md +++ b/src/guide/essentials/template-syntax.md @@ -1,32 +1,32 @@ -# Template Syntax {#template-syntax} +# Cú pháp Template {#template-syntax} - - Watch an interactive video lesson on Scrimba + + Xem video bài học tương tác trên Scrimba -Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers. +Vue sử dụng cú pháp template dựa trên HTML, cho phép bạn bind một cách khai báo (declarative) DOM được render với dữ liệu của instance component bên dưới. Tất cả template của Vue đều là HTML hợp lệ về mặt cú pháp, có thể được parse bởi các trình duyệt tuân thủ chuẩn và các HTML parser. -Under the hood, Vue compiles the templates into highly-optimized JavaScript code. Combined with the reactivity system, Vue can intelligently figure out the minimal number of components to re-render and apply the minimal amount of DOM manipulations when the app state changes. +Ở bên trong, Vue compile template thành mã JavaScript được tối ưu hóa cao. Kết hợp với hệ thống reactivity, Vue có thể thông minh xác định số lượng component tối thiểu cần re-render và áp dụng lượng thao tác DOM tối thiểu khi trạng thái ứng dụng thay đổi. -If you are familiar with Virtual DOM concepts and prefer the raw power of JavaScript, you can also [directly write render functions](/guide/extras/render-function) instead of templates, with optional JSX support. However, do note that they do not enjoy the same level of compile-time optimizations as templates. +Nếu bạn quen với khái niệm Virtual DOM và thích sức mạnh trực tiếp của JavaScript, bạn cũng có thể [viết trực tiếp render function](/guide/extras/render-function) thay vì template, với hỗ trợ JSX tùy chọn. Tuy nhiên, cần lưu ý rằng chúng không được hưởng mức tối ưu hóa compile-time như template. -## Text Interpolation {#text-interpolation} +## Nội suy văn bản {#text-interpolation} -The most basic form of data binding is text interpolation using the "Mustache" syntax (double curly braces): +Dạng binding dữ liệu cơ bản nhất là nội suy văn bản sử dụng cú pháp "Mustache" (dấu ngoặc nhọn kép): ```vue-html Message: {{ msg }} ``` -The mustache tag will be replaced with the value of the `msg` property [from the corresponding component instance](/guide/essentials/reactivity-fundamentals#declaring-reactive-state). It will also be updated whenever the `msg` property changes. +Thẻ mustache sẽ được thay thế bằng giá trị của property `msg` từ instance component tương ứng. Nó cũng sẽ được cập nhật mỗi khi property `msg` thay đổi. -## Raw HTML {#raw-html} +## HTML thô {#raw-html} -The double mustaches interpret the data as plain text, not HTML. In order to output real HTML, you will need to use the [`v-html` directive](/api/built-in-directives#v-html): +Mustache sẽ diễn giải dữ liệu như văn bản thuần, không phải HTML. Để render HTML thật, bạn cần dùng directive `v-html`: ```vue-html -

Using text interpolation: {{ rawHtml }}

-

Using v-html directive:

+

Dùng nội suy văn bản: {{ rawHtml }}

+

Dùng directive v-html:

```
-

Using text interpolation: {{ rawHtml }}

-

Using v-html directive:

+

Dùng nội suy văn bản: {{ rawHtml }}

+

Dùng directive v-html:

-Here we're encountering something new. The `v-html` attribute you're seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here, we're basically saying "keep this element's inner HTML up-to-date with the `rawHtml` property on the current active instance." +Ở đây chúng ta gặp một khái niệm mới. Attribute `v-html` bạn thấy được gọi là một **directive**. Directive được đặt tiền tố `v-` để chỉ ra rằng chúng là các attribute đặc biệt do Vue cung cấp, và như bạn đoán, chúng áp dụng hành vi reactive đặc biệt lên DOM được render. Ở đây, chúng ta đang nói rằng "hãy giữ inner HTML của phần tử này luôn đồng bộ với property `rawHtml` trên instance hiện tại." -The contents of the `span` will be replaced with the value of the `rawHtml` property, interpreted as plain HTML - data bindings are ignored. Note that you cannot use `v-html` to compose template partials, because Vue is not a string-based templating engine. Instead, components are preferred as the fundamental unit for UI reuse and composition. +Nội dung của thẻ `span` sẽ được thay thế bằng giá trị của property `rawHtml`, được diễn giải như HTML thuần - các data binding sẽ bị bỏ qua. Lưu ý rằng bạn không thể dùng `v-html` để ghép các phần template, vì Vue không phải là một template engine dựa trên string. Thay vào đó, component là đơn vị cơ bản để tái sử dụng và ghép UI. -:::warning Security Warning -Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS vulnerabilities](https://en.wikipedia.org/wiki/Cross-site_scripting). Only use `v-html` on trusted content and **never** on user-provided content. +:::warning Cảnh báo bảo mật +Việc render HTML động tùy ý trên website của bạn có thể rất nguy hiểm vì dễ dẫn đến lỗ hổng XSS. Chỉ sử dụng `v-html` với nội dung đáng tin cậy và **không bao giờ** dùng với nội dung do người dùng cung cấp. ::: -## Attribute Bindings {#attribute-bindings} +## Binding Attribute {#attribute-bindings} -Mustaches cannot be used inside HTML attributes. Instead, use a [`v-bind` directive](/api/built-in-directives#v-bind): +Mustache không thể dùng bên trong các attribute HTML. Thay vào đó, hãy dùng directive `v-bind`: ```vue-html
``` -The `v-bind` directive instructs Vue to keep the element's `id` attribute in sync with the component's `dynamicId` property. If the bound value is `null` or `undefined`, then the attribute will be removed from the rendered element. +Directive `v-bind` yêu cầu Vue giữ cho attribute `id` của phần tử luôn đồng bộ với property `dynamicId` của component. Nếu giá trị bind là `null` hoặc `undefined`, attribute sẽ bị xóa khỏi phần tử được render. -### Shorthand {#shorthand} +### Viết tắt {#shorthand} -Because `v-bind` is so commonly used, it has a dedicated shorthand syntax: +Vì `v-bind` được dùng rất thường xuyên, nó có cú pháp viết tắt riêng: ```vue-html
``` -Attributes that start with `:` may look a bit different from normal HTML, but it is in fact a valid character for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is optional, but you will likely appreciate it when you learn more about its usage later. +Các attribute bắt đầu bằng `:` có thể trông khác HTML thông thường, nhưng thực tế đây là ký tự hợp lệ cho tên attribute và tất cả trình duyệt mà Vue hỗ trợ đều parse đúng. Ngoài ra, chúng sẽ không xuất hiện trong markup cuối cùng. Cú pháp viết tắt là tùy chọn, nhưng bạn sẽ thấy nó tiện khi dùng nhiều. -> For the rest of the guide, we will be using the shorthand syntax in code examples, as that's the most common usage for Vue developers. +> Trong phần còn lại của hướng dẫn, chúng tôi sẽ dùng cú pháp viết tắt trong các ví dụ, vì đây là cách dùng phổ biến nhất với các developer Vue. -### Same-name Shorthand {#same-name-shorthand} +### Viết tắt cùng tên {#same-name-shorthand} -- Only supported in 3.4+ +- Chỉ hỗ trợ từ phiên bản 3.4 trở lên. -If the attribute has the same name as the variable name of the JavaScript value being bound, the syntax can be further shortened to omit the attribute value: +Nếu tên attribute giống với tên biến JavaScript được bind, bạn có thể rút gọn hơn nữa bằng cách bỏ giá trị: ```vue-html - +
- +
``` -This is similar to the property shorthand syntax when declaring objects in JavaScript. Note this is a feature that is only available in Vue 3.4 and above. +Cách này tương tự cú pháp shorthand khi khai báo object trong JavaScript. Lưu ý đây là tính năng chỉ có từ Vue 3.4 trở lên. -### Boolean Attributes {#boolean-attributes} +### Attribute boolean {#boolean-attributes} -[Boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes) are attributes that can indicate true / false values by their presence on an element. For example, [`disabled`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled) is one of the most commonly used boolean attributes. +Attribute boolean là các attribute biểu diễn true hoặc false thông qua việc có tồn tại hay không trên phần tử. Ví dụ `disabled` là một attribute phổ biến. -`v-bind` works a bit differently in this case: +`v-bind` hoạt động hơi khác trong trường hợp này: ```vue-html ``` -The `disabled` attribute will be included if `isButtonDisabled` has a [truthy value](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). It will also be included if the value is an empty string, maintaining consistency with `