Skip to content

Commit 6d99e9b

Browse files
committed
Merge branch 'master' into 2.0-cn
* master: Revert "Master" (#462) Master (#460) Update link to Karma (#458) Explicitly note on `events` opt deprecation (fixes #436) (#453) remove wording about not including a benchmark from the react team Set the background-color to white. (#452) fix capitalizion typo in migration guide add literal modifier for directives to migration guide fix typo in interpolation within attributes in migration guide update dynamic binding example in guide intro add named slot to render function guide add warnings about using arrow functions on instance properties (#448) fix links to single-file components - fixes #446 add note about two-way directives, linking to two-way filters example update two-filters example in migration guide Same word used multiple times in a sentence (#444) improve $data instance property API docs (#445) # Conflicts: # src/guide/index.md # src/guide/installation.md # src/guide/instance.md # src/guide/render-function.md # src/guide/unit-testing.md
2 parents 1a58494 + 5856735 commit 6d99e9b

File tree

9 files changed

+114
-79
lines changed

9 files changed

+114
-79
lines changed

src/api/index.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ type: api
347347
})
348348
```
349349

350+
<p class="tip">Note that __you should not use an arrow function with the `data` property__ (e.g. `data: () => { return { a: this.myProp }}`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.myProp` will be undefined.</p>
351+
350352
- **See also:** [Reactivity in Depth](/guide/reactivity.html)
351353

352354
### props
@@ -418,6 +420,8 @@ type: api
418420

419421
Computed properties to be mixed into the Vue instance. All getters and setters have their `this` context automatically bound to the Vue instance.
420422

423+
<p class="tip">Note that __you should not use an arrow function to define a computed property__ (e.g. `aDouble: () => this.a * 2`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.a` will be undefined.</p>
424+
421425
Computed properties are cached, and only re-computed on reactive dependency changes.
422426

423427
- **Example:**
@@ -458,6 +462,8 @@ type: api
458462

459463
Methods to be mixed into the Vue instance. You can access these methods directly on the VM instance, or use them in directive expressions. All methods will have their `this` context automatically bound to the Vue instance.
460464

465+
<p class="tip">Note that __you should not use an arrow function to define a method__ (e.g. `plus: () => this.a++`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.a` will be undefined.</p>
466+
461467
- **Example:**
462468

463469
```js
@@ -488,16 +494,18 @@ type: api
488494
``` js
489495
var vm = new Vue({
490496
data: {
491-
a: 1
497+
a: 1,
498+
b: 2,
499+
c: 3
492500
},
493501
watch: {
494-
'a': function (val, oldVal) {
502+
a: function (val, oldVal) {
495503
console.log('new: %s, old: %s', val, oldVal)
496504
},
497505
// string method name
498-
'b': 'someMethod',
506+
b: 'someMethod',
499507
// deep watcher
500-
'c': {
508+
c: {
501509
handler: function (val, oldVal) { /* ... */ },
502510
deep: true
503511
}
@@ -506,6 +514,8 @@ type: api
506514
vm.a = 2 // -> new: 2, old: 1
507515
```
508516

517+
<p class="tip">Note that __you should not use an arrow function to define a watcher__ (e.g. `searchQuery: newValue => this.updateAutocomplete(newValue)`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.updateAutocomplete` will be undefined.</p>
518+
509519
- **See also:** [Instance Methods - vm.$watch](#vm-watch)
510520

511521
## Options / DOM
@@ -559,6 +569,8 @@ type: api
559569

560570
## Options / Lifecycle Hooks
561571

572+
All lifecycle hooks automatically have their `this` context bound to the instance, so that you can access data, computed properties, and methods. This means __you should not use an arrow function to define a lifecycle method__ (e.g. `created: () => this.fetchTodos()`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.fetchTodos` will be undefined.
573+
562574
### beforeCreate
563575

564576
- **Type:** `Function`
@@ -829,7 +841,7 @@ type: api
829841

830842
- **Details:**
831843

832-
The data object that the Vue instance is observing. You can swap it with a new object. The Vue instance proxies access to the properties on its data object.
844+
The data object that the Vue instance is observing. The Vue instance proxies access to the properties on its data object.
833845

834846
- **See also:** [Options - data](#data)
835847

src/guide/comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ With that said, we hope you can feel confident in the fairness of the review bel
2626

2727
### Performance Profiles
2828

29-
In every real-world scenario that we've tested so far, Vue outperforms React by a fair margin (usually at least 20-50% faster, though in some cases much more than that). We could now link to benchmarks - but frankly, all benchmarks are flawed in some way and very few resemble what you'd write in a real application. Instead, let's break it down.
29+
In every real-world scenario that we've tested so far, Vue outperforms React by a fair margin. If your eyebrows are raising right now, read further. We'll breakdown why (and even include a benchmark developed in collaboration with the React team).
3030

3131
#### Render Performance
3232

src/guide/index.md

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
---
2-
title: 起步
2+
title: 介绍
33
type: guide
44
order: 2
55
---
66

7-
## Vue.js是什么?
7+
## What is Vue.js?
88

9-
Vue.js(读音 /vjuː/, 类似于 **view**)是一个构建数据驱动的 web 界面的库。Vue.js 的目标是通过尽可能简单的 API 实现**响应的数据绑定****组合的视图组件**
9+
Vue (pronounced /vjuː/, like **view**) is a **progressive framework** for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only, and is very easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with [modern tooling](single-file-components.html) and [supporting libraries](https://github.com/vuejs/awesome-vue#libraries--plugins).
1010

11-
Vue.js 自身不是一个全能框架——它只聚焦于视图层。因此它非常容易学习,`非常容易`与其它库或已有项目整合。另一方面,在与[相关工具](application.html)[支持库](https://github.com/vuejs/awesome-vue#libraries--plugins)一起使用时,Vue.js 也能完美地驱动复杂的单页应用。
11+
If you are an experienced frontend developer and want to know how Vue compares to other libraries/frameworks, check out the [Comparison with Other Frameworks](comparison.html).
1212

13-
如果你是有经验的前端开发者,想知道 Vue.js 与其它库/框架的区别,查看[对比其它框架](comparison.html)
13+
## Getting Started
1414

15-
## 起步
15+
The easiest way to try out Vue.js is using the [JSFiddle Hello World example](https://jsfiddle.net/chrisvfritz/4tpzm3e1/). Feel free to open it in another tab and follow along as we go through some basic examples. If you prefer downloading / installing from a package manager, check out the [Installation](/guide/installation.html) page.
1616

17-
尝试 Vue.js 最简单的方法是使用 [JSFiddle Hello World example](https://jsfiddle.net/chrisvfritz/4tpzm3e1/) 。在浏览器新标签页中打开它,跟着我们查看一些基础示例。如果你喜欢用包管理器下载/安装,查看[安装](/guide/installation.html)教程。
17+
## Declarative Rendering
1818

19-
## 声明式渲染
19+
At the core of Vue.js is a system that enables us to declaratively render data to the DOM using straightforward template syntax:
2020

21-
Vue.js的核心系统使我们能以声明的方式使用简单的模板语法渲染数据到DOM中:
22-
23-
24-
```html
21+
``` html
2522
<div id="app">
2623
{{ message }}
2724
</div>
2825
```
29-
3026
``` js
3127
var app = new Vue({
3228
el: '#app',
@@ -35,7 +31,6 @@ var app = new Vue({
3531
}
3632
})
3733
```
38-
3934
{% raw %}
4035
<div id="app" class="demo">
4136
{{ message }}
@@ -50,46 +45,48 @@ var app = new Vue({
5045
</script>
5146
{% endraw %}
5247

53-
我们已经创建了第一个Vue应用!直接渲染字一个模板非常简单,但Vue.js在背后做了大量工作。DOM 会自动响应数据的变化。怎么确定?我们如何知道?打开你的浏览器的控制台,修改`app.message`,你将看到上例相应地更新。
48+
We have already created our very first Vue app! This looks pretty similar to just rendering a string template, but Vue has done a lot of work under the hood. The data and the DOM are now linked, and everything is now **reactive**. How do we know? Just open up your browser's JavaScript console and set `app.message` to a different value. You should see the rendered example above update accordingly.
5449

55-
除了文本插值,也可以绑定元素属性像这样:
50+
In addition to text interpolation, we can also bind element attributes like this:
5651

5752
``` html
5853
<div id="app-2">
59-
<span v-bind:id="id">Inspect me</span>
54+
<span v-bind:title="message">
55+
Hover your mouse over me for a few seconds to see my dynamically bound title!
56+
</span>
6057
</div>
6158
```
62-
6359
``` js
6460
var app2 = new Vue({
6561
el: '#app-2',
6662
data: {
67-
id: 'inspect-me'
63+
message: 'You loaded this page on ' + new Date()
6864
}
6965
})
7066
```
71-
7267
{% raw %}
7368
<div id="app-2" class="demo">
74-
<span v-bind:id="id">Inspect me</span>
69+
<span v-bind:title="message">
70+
Hover your mouse over me for a few seconds to see my dynamically bound title!
71+
</span>
7572
</div>
7673
<script>
7774
var app2 = new Vue({
7875
el: '#app-2',
7976
data: {
80-
id: 'inspect-me'
77+
message: 'You loaded this page on ' + new Date()
8178
}
8279
})
8380
</script>
8481
{% endraw %}
8582

86-
这里我们遇到新东西。你看到的 `v-bind` 属性是一个***指令***。指令带有前缀 v-,以指示它们是 Vue.js 提供的特殊特性。并且如你所想象的,它们会对绑定的目标元素添加响应式的特殊行为。这里基本上可以看作“绑定这个元素的`id`”属性为Vue实例中的`id`
83+
Here we are encountering something new. The `v-bind` attribute you are 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 it is basically saying "keep this element's `title` attribute up-to-date with the `message` property on the Vue instance."
8784

88-
使用浏览器调试工具定位再元素上 - 可以看到这有个id`inspect-me`。同样,可以在控制台修改 `app2.id`
85+
If you open up your JavaScript console again and enter `app2.message = 'some new message'`, you'll once again see that the bound HTML - in this case the `title` attribute - has been updated.
8986

90-
## 条件和循环
87+
## Conditionals and Loops
9188

92-
切换显示元素也是相当简单:
89+
It's quite simple to toggle the presence of an element, too:
9390

9491
``` html
9592
<div id="app-3">
@@ -120,22 +117,21 @@ var app3 = new Vue({
120117
</script>
121118
{% endraw %}
122119

123-
在控制台设置 `app3.seen = false`,可以看到消息消失了。
120+
Go ahead and enter `app3.seen = false` in the console. You should see the message disappear.
124121

125-
示例演示了我们不仅可以绑定数据到文本和属性,也可以绑定到DOM结构上。甚至,Vue还提供一套强力的过渡系统在元素通过Vue插入/更新/移除自动应用[过渡效果](transitions.html)
122+
This example demonstrates that we can bind data to not only text and attributes, but also the **structure** of the DOM. Moreover, Vue also provides a powerful transition effect system that can automatically apply [transition effects](transitions.html) when elements are inserted/updated/removed by Vue.
126123

127-
这里有一些其他指令,每一个都有自己独特的函数。例如,`v-for`指令可以被用来从数组数据中展示列表元素。
124+
There are quite a few other directives, each with its own special functionality. For example, the `v-for` directive can be used for displaying a list of items using the data from an Array:
128125

129126
``` html
130127
<div id="app-4">
131128
<ol>
132129
<li v-for="todo in todos">
133130
{{ todo.text }}
134131
</li>
135-
</ul>
132+
</ol>
136133
</div>
137134
```
138-
139135
``` js
140136
var app4 = new Vue({
141137
el: '#app-4',
@@ -148,7 +144,6 @@ var app4 = new Vue({
148144
}
149145
})
150146
```
151-
152147
{% raw %}
153148
<div id="app-4" class="demo">
154149
<ol>
@@ -171,19 +166,18 @@ var app4 = new Vue({
171166
</script>
172167
{% endraw %}
173168

174-
在控制台输入 `app4.todos.push({ text: 'New item' })`。可以看到新元素进了列表中。
169+
In the console, enter `app4.todos.push({ text: 'New item' })`. You should see a new item appended to the list.
175170

176-
## 处理用户输入
171+
## Handling User Input
177172

178-
为了让用户与应用互动,我们用`v-on`指令加上事件监听来触发Vue实例中的方法:
173+
To let users interact with your app, we can use the `v-on` directive to attach event listeners that invoke methods on our Vue instances:
179174

180175
``` html
181176
<div id="app-5">
182177
<p>{{ message }}</p>
183178
<button v-on:click="reverseMessage">Reverse Message</button>
184179
</div>
185180
```
186-
187181
``` js
188182
var app5 = new Vue({
189183
el: '#app-5',
@@ -197,7 +191,6 @@ var app5 = new Vue({
197191
}
198192
})
199193
```
200-
201194
{% raw %}
202195
<div id="app-5" class="demo">
203196
<p>{{ message }}</p>
@@ -218,17 +211,16 @@ var app5 = new Vue({
218211
</script>
219212
{% endraw %}
220213

221-
注意在方法中我们没有触碰DOM简单地更新了我们应用状态 - 所有DOM操作都由Vue来处理,我们可以聚焦在潜在的逻辑代码上。
214+
Note in the method we simply update the state of our app without touching the DOM - all DOM manipulations are handled by Vue, and the code you write is focused on the underlying logic.
222215

223-
Vue还提供了`v-model` 指令 很轻巧地实现输入和应用状态间的双向数据绑定。
216+
Vue also provides the `v-model` directive that makes two-way binding between form input and app state a breeze:
224217

225218
``` html
226219
<div id="app-6">
227220
<p>{{ message }}</p>
228221
<input v-model="message">
229222
</div>
230223
```
231-
232224
``` js
233225
var app6 = new Vue({
234226
el: '#app-6',
@@ -237,7 +229,6 @@ var app6 = new Vue({
237229
}
238230
})
239231
```
240-
241232
{% raw %}
242233
<div id="app-6" class="demo">
243234
<p>{{ message }}</p>
@@ -253,13 +244,13 @@ var app6 = new Vue({
253244
</script>
254245
{% endraw %}
255246

256-
## 构建组件
247+
## Composing with Components
257248

258-
组件系统是Vue中另一个重要的概念,因为它是一个抽象,使我们能够通过建立一些小型的,独立的,经常可复用的组件组成大型应用。几乎任何类型的应用界面都可以抽象成一棵组件树🌲:
249+
The component system is another important concept in Vue, because it's an abstraction that allows us to build large-scale applications composed of small, self-contained, and often reusable components. If we think about it, almost any type of application interface can be abstracted into a tree of components:
259250

260-
![组件树](/images/components.png)
251+
![Component Tree](/images/components.png)
261252

262-
Vue中,一个组件基本上是一个预定义了设置的Vue实例。在Vue注册组件也很简单:
253+
In Vue, a component is essentially a Vue instance with pre-defined options. Registering a component in Vue is straightforward:
263254

264255
``` js
265256
// Define a new component called todo-item
@@ -268,7 +259,7 @@ Vue.component('todo-item', {
268259
})
269260
```
270261

271-
现在可以在另一个组件模板中使用它:
262+
Now you can compose it in another component's template:
272263

273264
``` html
274265
<ul>
@@ -280,7 +271,7 @@ Vue.component('todo-item', {
280271
</ul>
281272
```
282273

283-
不过每个Todo都渲染一样的文本并不有趣。我们可以从父组件传递数据到子组件中。让我们修改组件定义,使它可以结构一个 [prop](/guide/components.html#Props)
274+
But this would render the same text for every todo, which is not super interesting. We should be able to pass data from the parent scope into child components. Let's modify the component definition to make it accept a [prop](/guide/components.html#Props):
284275

285276
``` js
286277
Vue.component('todo-item', {
@@ -292,7 +283,7 @@ Vue.component('todo-item', {
292283
})
293284
```
294285

295-
现在我们可以通过`v-bind`传递todo进入每个可重用的组件中:
286+
Now we can pass the todo into each repeated component using `v-bind`:
296287

297288
``` html
298289
<div id="app-7">
@@ -341,9 +332,9 @@ var app7 = new Vue({
341332
</script>
342333
{% endraw %}
343334

344-
这只是一个例子,不过我们已经成功将应用分成两个更小的单元,并且子组件可以与父组件通过props很好地解耦。我们现在可以进一步使用复杂的模板和不影响父组件的逻辑改良`<todo-item>`组件
335+
This is just a contrived example, but we have managed to separate our app into two smaller units, and the child is reasonably well-decoupled from the parent via the props interface. We can now further improve our `<todo-item>` component with more complex template and logic without affecting the parent app.
345336

346-
在大型应用中,很有必要把整个应用拆分使开发更好管理。更多关于组件的内容在[后面的指南](/guide/components.html),这里有个类似的示例展示应用的模板可以像这样:
337+
In a large application, it is necessary to divide the whole app into components to make development manageable. We will talk a lot more about components [later in the guide](/guide/components.html), but here's an (imaginary) example of what an app's template might look like with components:
347338

348339
``` html
349340
<div id="app">
@@ -355,15 +346,14 @@ var app7 = new Vue({
355346
</div>
356347
```
357348

358-
### 关于自定义元素
359-
360-
可以注意到Vue组件很类似于**自定义元素**,遵循了[Web组件规范](http://www.w3.org/wiki/WebComponents/)。这是因为Vue的组件语法模仿了规范。例如,Vue组件实现的[Slot API](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.md)`is`特性。不过也有些关键不同点:
349+
### Relation to Custom Elements
361350

362-
1.Web 组件规范仍然远未完成,并且没有浏览器实现。相比之下,Vue.js 组件不需要任何补丁,并且在所有支持的浏览器(IE9 及更高版本)之下表现一致。必要时,Vue.js 组件也可以放在原生自定义元素之内。
351+
You may have noticed that Vue components are very similar to **Custom Elements**, which are part of the [Web Components Spec](http://www.w3.org/wiki/WebComponents/). That's because Vue's component syntax is loosely modeled after the spec. For example, Vue components implement the [Slot API](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Slots-Proposal.md) and the `is` special attribute. However, there are a few key differences:
363352

364-
2.Vue.js 组件提供了原生自定义元素所不具备的一些重要功能,比如组件间的数据流,自定义事件系统,以及构建工具的继承。
353+
1. The Web Components Spec is still in draft status, and is not natively implemented in every browser. In comparison, Vue components don't require any polyfills and work consistently in all supported browsers (IE9 and above). When needed, Vue components can also be wrapped inside a native custom element.
365354

355+
2. Vue components provide important features that are not available in plain custom elements, most notably cross-component data flow, custom event communication and build tool integrations.
366356

367-
## 准备好了吗?
357+
## Ready for More?
368358

369-
我们才简要介绍了Vue核心最基本的功能 - 其余指南将涵盖它们,并提供进一步的功能细节,所以请确保读完全部内容。
359+
We've just briefly introduced the most basic features of Vue.js core - the rest of this guide will cover them and other advanced features with much finer details, so make sure to read through it all!

src/guide/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Also available on [jsdelivr](//cdn.jsdelivr.net/vue/{{vue_version}}/vue.js) or [
3636

3737
## NPM
3838

39-
在用 Vue.js 构建大型应用时推荐使用 NPM 安装,NPM 能很好地和诸如 [Webpack](http://webpack.github.io/)[Browserify](http://browserify.org/) 的 CommonJS 模块打包器配合使用。Vue.js 也提供配套工具来开发[单文件组件](application.html#单文件组件)
39+
在用 Vue.js 构建大型应用时推荐使用 NPM 安装,NPM 能很好地和诸如 [Webpack](http://webpack.github.io/)[Browserify](http://browserify.org/) 模块打包器配合使用。Vue.js 也提供配套工具来开发[单文件组件](single-file-components.html)
4040

4141
``` bash
4242
# 最新稳定版

src/guide/instance.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ vm.$watch('a', function (newVal, oldVal) {
7777
})
7878
```
7979

80-
参考 [API 文档](/api)查看全部的实例属性与方法。
80+
81+
<p class="tip">Note that __you should not use arrow functions on an instance property or callback__ (e.g. `vm.$watch('a', newVal => this.myMethod())`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.myMethod` will be undefined.</p>
82+
83+
Consult the [API reference](/api) for the full list of instance properties and methods.
8184

8285
## 实例生命周期
8386

0 commit comments

Comments
 (0)