Skip to content

Commit d49e473

Browse files
authored
Merge pull request #74 from daix6/2.0-cn
guide syntax
2 parents 1f6e110 + 88a69cd commit d49e473

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

src/guide/syntax.md

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,59 @@ type: guide
44
order: 4
55
---
66

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 解析器解析。
88

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 操作上。
1010

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 语法。
1212

13-
## Interpolations
13+
## 插值
1414

15-
### Text
15+
### 文本
1616

17-
The most basic form of data binding is text interpolation using the "Mustache" syntax (double curly braces):
17+
数据绑定最常见的形式就是使用 “Mustache” 语法(双大括号)的文本插值:
1818

1919
``` html
2020
<span>Message: {{ msg }}</span>
2121
```
2222

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.
23+
Mustache 标签将会被替代为对应数据对象上 `msg` 属性的值。无论合适绑定的数据对象上 `msg` 属性发生了改变,插值处的内容都会更新。
2424

25-
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:
25+
通过使用 [v-once 指令](/api/#v-once),你也能执行一次性地插值,当数据改变时,插值处的内容不会更新。但请留心这会影响到该节点上所有的数据绑定:
2626

2727
``` html
2828
<span v-once>This will never change: {{ msg }}</span>
2929
```
3030

31-
### Raw HTML
31+
### HTML
3232

33-
The double mustaches interprets the data as plain text, not HTML. In order to output real HTML, you will need to use the `v-html` directive:
33+
双大括号会将数据解释为纯文本,而非 HTML。为了输出真正的 HTML,你需要使用 `v-html` 指令:
3434

3535
``` html
3636
<div v-html="rawHtml"></div>
3737
```
3838

39-
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 重用与复合的基本单元。
4040

41-
<p class="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+
<p class="tip">你的站点上动态渲染的任意 HTML 可能会非常危险,因为它很容易导致 [XSS 攻击](https://en.wikipedia.org/wiki/Cross-site_scripting)。请只对可信内容使用 HTML 插值,**绝不要**对用户提供的内容插值。</p>
4242

43-
### Attributes
43+
### 属性
4444

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)
4646

4747
``` html
4848
<div v-bind:id="dynamicId"></div>
4949
```
5050

51-
It also works for boolean attributes - the attribute will be removed if the condition evaluates to a falsy value:
51+
这对布尔值的属性也有效 —— 如果条件被求值为 false 的话该属性会被移除:
5252

5353
``` html
5454
<button v-bind:disabled="someDynamicCondition">Button</button>
5555
```
5656

57-
### Using JavaScript Expressions
57+
### 使用 JavaScript 表达式
5858

59-
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:
59+
迄今为止,在我们的模板中,我们一直都只绑定简单的属性键值。但实际上,对于所有的数据绑定,Vue.js 都提供了完全的 JavaScript 表达式支持。
6060

6161
``` html
6262
{{ number + 1 }}
@@ -68,29 +68,31 @@ So far we've only been binding to simple property keys in our templates. But Vue
6868
<div v-bind:id="'list-' + id"></div>
6969
```
7070

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:
71+
这些表达式会在所属 Vue 实例的数据作用域下作为 JavaScript 被解析。有个限制就是,每个绑定都只能包含**单个表达式**,所以下面的例子都**不会**生效。
7272

7373
``` html
74-
<!-- this is a statement, not an expression: -->
74+
<!-- 这是语句,不是表达式 -->
7575
{{ var a = 1 }}
7676

77-
<!-- flow control won't work either, use ternary expressions -->
77+
<!-- 流控制也不会生效,请使用三元表达式 -->
7878
{{ if (ok) { return message } }}
7979
```
8080

81+
<p class="tip">模板表达式都被放在沙盒中,只能访问全局变量的一个白名单,如 `Math` 和 `Date`。你不应该在模板表达式中视图访问用户定义的全局变量。</p>
82+
8183
<p class="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>
8284

83-
### Filters
85+
### 过滤器
8486

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:
87+
Vue.js 允许你自定义过滤器,被用作一些常见的文本格式化。过滤器应该被添加在 **mustache 插值**的尾部,由“管道符”指示:
8688

8789
``` html
8890
{{ message | capitalize }}
8991
```
9092

91-
<p class="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>
93+
<p class="tip">Vue 2.x 中,过滤器只能在 mustache 绑定中使用。为了在指令绑定中实现同样的行为,你应该使用[计算属性](/guide/computed.html)</p>
9294

93-
The filter function always receives the expression's value as the first argument.
95+
过滤器函数总接受表达式的值作为第一个参数。
9496

9597
``` js
9698
new Vue({
@@ -105,84 +107,83 @@ new Vue({
105107
})
106108
```
107109

108-
Filters can be chained:
110+
过滤器可以串联:
109111

110112
``` html
111113
{{ message | filterA | filterB }}
112114
```
113115

114-
Filters are JavaScript functions, therefore they can take arguments:
116+
过滤器是 JavaScript 函数,因此可以接受参数:
115117

116118
``` html
117119
{{ message | filterA('arg1', arg2) }}
118120
```
119121

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.
122+
这里,字符串 `'arg1'` 将传给过滤器作为第二个参数,`arg2` 表达式的值将被求值然后传给过滤器作为第三个参数。
121123

122-
## Directives
124+
## 指令
123125

124-
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 上。让我们回顾一下在介绍里的例子:
125127

126128
``` html
127129
<p v-if="seen">Now you see me</p>
128130
```
129131

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>` 元素。
131133

132-
### Arguments
134+
### 参数
133135

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 属性:
135137

136138
``` html
137139
<a v-bind:href="url"></a>
138140
```
139141

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`.
142+
在这里 `href` 是参数,告知 `v-bind` 指令将该元素的 `href` 属性与表达式 `url` 的值绑定。
141143

142-
Another example is the `v-on` directive, which listens to DOM events:
144+
另一个例子是 `v-on` 指令,它用于监听 DOM 事件:
143145

144146
``` html
145147
<a v-on:click="doSomething">
146148
```
147149

148-
Here the argument is the event name to listen to. We will talk about event handling in more detail too.
150+
在这里参数是监听的事件名。我们也会更详细地讨论事件处理。
149151

150-
### Modifiers
152+
### 修饰符
151153

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:
154+
修饰符(Modifiers)是以半角句号 `.` 指明的特殊后缀,用于指出一个指定应该以特殊方式绑定。例如,`.prevent` 装饰符告诉 `v-on` 指令对于触发的事件调用 `event.preventDefault()`
153155

154156
``` html
155157
<form v-on:submit.prevent="onSubmit"></form>
156158
```
157159

158-
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`时,会看到更多装饰符的使用。
159161

160-
## Shorthands
162+
## 缩写
161163

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`:
164+
`v-` 前缀可作为一种模板中标识 Vue 特性的视觉暗示。当你使用 Vue.js 为现有的标记添加动态行为时,它会很有用,但对于一些频繁使用的指令来说略显冗余。同时,当搭建 Vue.js 管理所有模板的 [SPA](https://en.wikipedia.org/wiki/Single-page_application) 时,`v-` 前缀也变得没那么重要了。因此,Vue.js 为两个最为常用的指令提供了特别的缩写:
163165

164-
### `v-bind` Shorthand
166+
### `v-bind` 缩写
165167

166168
``` html
167-
<!-- full syntax -->
169+
<!-- 完整语法 -->
168170
<a v-bind:href="url"></a>
169171

170-
<!-- shorthand -->
172+
<!-- 缩写 -->
171173
<a :href="url"></a>
172174
```
173175

174-
175-
### `v-on` Shorthand
176+
### `v-on` 缩写
176177

177178
``` html
178-
<!-- full syntax -->
179+
<!-- 完整缩写 -->
179180
<a v-on:click="doSomething"></a>
180181

181-
<!-- shorthand -->
182+
<!-- 缩写 -->
182183
<a @click="doSomething"></a>
183184
```
184185

185-
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 的浏览器都能被正确地解析。而且,它们不会出现在最终渲染的标记。缩写语法是完全可选的,但随着你更深入地了解它们的作用,你会庆幸拥有它们。
186187

187188
***
188189

0 commit comments

Comments
 (0)