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/syntax.md
+49-48Lines changed: 49 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -4,59 +4,59 @@ type: guide
4
4
order: 4
5
5
---
6
6
7
-
Vue.js uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying Vue instance's data. All Vue.js templates are valid HTML that can be parsed by spec-compliant browsers and HTML parsers.
7
+
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据。所有 Vue.js 的模板都是合法的 HTML,所以能被遵循规范的浏览器和 HTML 解析器解析。
8
8
9
-
Under the hood, Vue compiles the templates into Virtual DOM render functions. Combined with the reactivity system, Vue is able to intelligently figure out the minimal amount of components to re-render and apply the minimal amount of DOM manipulations when the app state changes.
9
+
在底层的实现上,Vue 将模板编译成虚拟 DOM 渲染函数。结合响应系统,在应用状态改变时,Vue 能够智能地计算出重新渲染组件的最小代价并应用到 DOM 操作上。
10
10
11
-
If you are familiar with Virtual DOM concepts and prefer the raw power of JavaScript, you can also [directly write render functions](/guide/render-function.html) instead of templates, with optional JSX support.
11
+
如果你熟悉虚拟 DOM 并且偏爱 JavaScript 的原始力量,你也可以不用模板,[直接写渲染(render)函数](/guide/render-function.html),使用可选的 JSX 语法。
12
12
13
-
## Interpolations
13
+
## 插值
14
14
15
-
### Text
15
+
### 文本
16
16
17
-
The most basic form of data binding is text interpolation using the "Mustache" syntax (double curly braces):
17
+
数据绑定最常见的形式就是使用 “Mustache” 语法(双大括号)的文本插值:
18
18
19
19
```html
20
20
<span>Message: {{ msg }}</span>
21
21
```
22
22
23
-
The mustache tag will be replaced with the value of the `msg`property on the corresponding data object. It will also be updated whenever the data object's `msg`property changes.
You can also perform one-time interpolations that do not update on data change by using the [v-once directive](/api/#v-once), but keep in mind this will also affect any binding on the same node:
The contents are inserted 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.
39
+
被插入的内容都会被当做 HTML —— 数据绑定会被忽略。注意,你不能使用 `v-html`来复合局部模板,因为 Vue 不是基于字符串的模板引擎。组件更适合担任 UI 重用与复合的基本单元。
40
40
41
-
<pclass="tip">Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). Only use HTML interpolation on trusted content and **never** on user-provided content.</p>
41
+
<pclass="tip">你的站点上动态渲染的任意 HTML 可能会非常危险,因为它很容易导致 [XSS 攻击](https://en.wikipedia.org/wiki/Cross-site_scripting)。请只对可信内容使用 HTML 插值,**绝不要**对用户提供的内容插值。</p>
42
42
43
-
### Attributes
43
+
### 属性
44
44
45
-
Mustaches cannot be used inside HTML attributes, instead use a [v-bind directive](/api/#v-bind):
45
+
Mustache 不能在 HTML 属性中使用,应使用 [v-bind 指令](/api/#v-bind):
46
46
47
47
```html
48
48
<divv-bind:id="dynamicId"></div>
49
49
```
50
50
51
-
It also works for boolean attributes - the attribute will be removed if the condition evaluates to a falsy value:
So far we've only been binding to simple property keys in our templates. But Vue.js actually supports the full power of JavaScript expressions inside all data bindings:
@@ -68,29 +68,31 @@ So far we've only been binding to simple property keys in our templates. But Vue
68
68
<divv-bind:id="'list-' + id"></div>
69
69
```
70
70
71
-
These expressions will be evaluated as JavaScript in the data scope of the owner Vue instance. One restriction is that each binding can only contain **one single expression**, so the following will **NOT** work:
<pclass="tip">Template expressions are sandboxed and only have access to a whitelist of globals such as `Math` and `Date`. You should not attempt to access user defined globals in template expressions.</p>
82
84
83
-
### Filters
85
+
### 过滤器
84
86
85
-
Vue.js allows you to define filters that can be used to apply common text formatting. Filters should be appended to the end of a **mustache interpolation**, denoted by the "pipe" symbol:
<pclass="tip">Vue 2.x filters can only be used inside mustache bindings. To achieve the same behavior inside directive bindings, you should use [Computed properties](/guide/computed.html) instead.</p>
The filter function always receives the expression's value as the first argument.
95
+
过滤器函数总接受表达式的值作为第一个参数。
94
96
95
97
```js
96
98
newVue({
@@ -105,84 +107,83 @@ new Vue({
105
107
})
106
108
```
107
109
108
-
Filters can be chained:
110
+
过滤器可以串联:
109
111
110
112
```html
111
113
{{ message | filterA | filterB }}
112
114
```
113
115
114
-
Filters are JavaScript functions, therefore they can take arguments:
116
+
过滤器是 JavaScript 函数,因此可以接受参数:
115
117
116
118
```html
117
119
{{ message | filterA('arg1', arg2) }}
118
120
```
119
121
120
-
Here, the plain string `'arg1'`will be passed into the filter as the second argument, and the value of expression `arg2`will be evaluated and passed in as the third argument.
Directives are special attributes with the `v-`prefix. Directive attribute values are expected to be **a single JavaScript expression** (with the exception for `v-for`, which will be discussed later). A directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Let's review the example we saw in the introduction:
126
+
指令(Directives)是带有 `v-`前缀的特殊属性。指令属性的值预期是**单一 JavaScript 表达式**(除了 `v-for`,之后再讨论)。指令的职责就是当其表达式的值改变时响应地将某些行为应用到 DOM 上。让我们回顾一下在介绍里的例子:
125
127
126
128
```html
127
129
<pv-if="seen">Now you see me</p>
128
130
```
129
131
130
-
Here, the `v-if`directive would remove/insert the `<p>`element based on the truthiness of the value of the expression `seen`.
132
+
这里,`v-if`指令将根据表达式 `seen` 的值的真假来移除/插入 `<p>`元素。
131
133
132
-
### Arguments
134
+
### 参数
133
135
134
-
Some directives can take an "argument", denoted by a colon after the directive name. For example, the `v-bind`directive is used to reactively update an HTML attribute:
136
+
一些指令能接受一个“参数”,在指令后以冒号指明。例如,`v-bind`指令被用来响应地更新 HTML 属性:
135
137
136
138
```html
137
139
<av-bind:href="url"></a>
138
140
```
139
141
140
-
Here`href`is the argument, which tells the `v-bind`directive to bind the element's `href`attribute to the value of the expression `url`.
Another example is the `v-on`directive, which listens to DOM events:
144
+
另一个例子是 `v-on`指令,它用于监听 DOM 事件:
143
145
144
146
```html
145
147
<av-on:click="doSomething">
146
148
```
147
149
148
-
Here the argument is the event name to listen to. We will talk about event handling in more detail too.
150
+
在这里参数是监听的事件名。我们也会更详细地讨论事件处理。
149
151
150
-
### Modifiers
152
+
### 修饰符
151
153
152
-
Modifiers are special postfixes denoted by a dot, which indicate that a directive should be bound in some special way. For example, the `.prevent`modifier tells the `v-on`directive to call `event.preventDefault()` on the triggered event:
We will see more use of modifiers later when we take a more thorough look at `v-on`and`v-model`.
160
+
之后当我们更深入地了解 `v-on`与`v-model`时,会看到更多装饰符的使用。
159
161
160
-
## Shorthands
162
+
## 缩写
161
163
162
-
The `v-`prefix serves as a visual cue for identifying Vue-specific attributes in your templates. This is useful when you are using Vue.js to apply dynamic behavior to some existing markup, but can feel verbose for some frequently used directives. At the same time, the need for the `v-` prefix becomes less important when you are building an [SPA](https://en.wikipedia.org/wiki/Single-page_application)where Vue.js manages every template. Therefore, Vue.js provides special shorthands for two of the most often used directives, `v-bind` and `v-on`:
They may look a bit different from normal HTML, but`:`and`@`are valid chars for attribute names and all Vue.js supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.
186
+
它们看起来可能与普通的 HTML 略有不同,但`:`与`@`对于属性名来说都是合法字符,在所有支持 Vue.js 的浏览器都能被正确地解析。而且,它们不会出现在最终渲染的标记。缩写语法是完全可选的,但随着你更深入地了解它们的作用,你会庆幸拥有它们。
0 commit comments