Skip to content

Commit dbb8a77

Browse files
committed
api/index.md
1 parent 53e5d9f commit dbb8a77

File tree

1 file changed

+3
-57
lines changed

1 file changed

+3
-57
lines changed

src/api/index.md

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,15 @@ type: api
101101
- **用法:**
102102

103103
使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象。
104-
Create a "subclass" of the base Vue constructor. The argument should be an object containing component options.
105104

106105
`data` 选项是特例,需要注意 - 在 `Vue.extend()` 中它必须是函数
107-
The special case to note here is the `data` option - it must be a function when used with `Vue.extend()`.
108106

109107
``` html
110108
<div id="mount-point"></div>
111109
```
112110

113111
``` js
114112
// 创建构造器
115-
// create constructor
116113
var Profile = Vue.extend({
117114
template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
118115
data: function () {
@@ -124,12 +121,10 @@ type: api
124121
}
125122
})
126123
// 创建 Profile 实例,并挂载到一个元素上。
127-
// create an instance of Profile and mount it on an element
128124
new Profile().$mount('#mount-point')
129125
```
130126

131127
结果如下:
132-
Will result in:
133128

134129
``` html
135130
<p>Walter White aka Heisenberg</p>
@@ -146,17 +141,13 @@ type: api
146141
- **用法:**
147142

148143
在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。
149-
Defer the callback to be executed after the next DOM update cycle. Use it immediately after you've changed some data to wait for the DOM update.
150144

151145
``` js
152146
// 修改数据
153-
// modify data
154147
vm.msg = 'Hello'
155148
// DOM 还没有更新
156-
// DOM not updated yet
157149
Vue.nextTick(function () {
158150
// DOM 更新了
159-
// DOM updated
160151
})
161152
```
162153

@@ -174,10 +165,8 @@ type: api
174165
- **用法:**
175166

176167
设置对象的属性。如果对象是响应式的,确保属性被创建后也是响应式的,同时触发视图更新。这个方法主要用于避开 Vue 不能检测属性被添加的限制。
177-
Set a property on an object. If the object is reactive, ensure the property is created as a reactive property and trigger view updates. This is primarily used to get around the limitation that Vue cannot detect property additions.
178168

179169
**注意对象不能是 Vue 实例,或者 Vue 实例的根数据对象**
180-
**Note the object cannot be a Vue instance, or the root data object of a Vue instance.**
181170

182171
- **参考:** [深入响应式原理](/guide/reactivity.html)
183172

@@ -190,10 +179,8 @@ type: api
190179
- **用法:**
191180

192181
删除对象的属性。如果对象是响应式的,确保删除更新视图的触发器。这个方法主要用于避开 Vue 不能检测到属性被删除的限制,但是你应该很少会使用它。
193-
Delete a property on an object. If the object is reactive, ensure the deletion triggers view updates. This is primarily used to get around the limitation that Vue cannot detect property deletions, but you should rarely need to use it.
194182

195183
**注意对象不能是 Vue 实例,或者 Vue 实例的根数据对象**
196-
**Note the object cannot be a Vue instance, or the root data object of a Vue instance.**
197184

198185
- **参考:** [深入响应式原理](/guide/reactivity.html)
199186

@@ -206,11 +193,9 @@ type: api
206193
- **用法:**
207194

208195
注册或获取全局指令。
209-
Register or retrieve a global directive.
210196

211197
``` js
212198
// 注册
213-
// register
214199
Vue.directive('my-directive', {
215200
bind: function () {},
216201
inserted: function () {},
@@ -220,14 +205,11 @@ type: api
220205
})
221206

222207
// 注册(传入一个简单的指令函数)
223-
// register (simple function directive)
224208
Vue.directive('my-directive', function () {
225209
// 这里将会被 `bind` 和 `update` 调用
226-
// this will be called as `bind` and `update`
227210
})
228211

229212
// getter,返回已注册的指令
230-
// getter, return the directive definition if registered
231213
var myDirective = Vue.directive('my-directive')
232214
```
233215

@@ -242,17 +224,14 @@ type: api
242224
- **用法:**
243225

244226
注册或获取全局过滤器。
245-
Register or retrieve a global filter.
246227

247228
``` js
248229
// 注册
249-
// register
250230
Vue.filter('my-filter', function (value) {
251-
// return processed value
231+
// 返回处理后的值
252232
})
253233

254234
// getter,返回已注册的过滤器
255-
// getter, return the filter if registered
256235
var myFilter = Vue.filter('my-filter')
257236
```
258237

@@ -265,19 +244,15 @@ type: api
265244
- **用法:**
266245

267246
注册或获取全局组件。
268-
Register or retrieve a global component.
269247

270248
``` js
271249
// 注册组件,传入一个扩展过的构造器
272-
// register an extended constructor
273250
Vue.component('my-component', Vue.extend({ /* ... */ }))
274251

275252
// 注册组件,传入一个选项对象(自动调用 Vue.extend)
276-
// register an options object (automatically call Vue.extend)
277253
Vue.component('my-component', { /* ... */ })
278254

279255
// 获取注册的组件(始终返回构造器)
280-
// retrieve a registered component (always return constructor)
281256
var MyComponent = Vue.component('my-component')
282257
```
283258

@@ -291,10 +266,8 @@ type: api
291266
- **用法:**
292267

293268
安装 Vue.js 插件。如果插件是一个对象,必须提供 `install` 方法。如果插件是一个函数,它会被作为 install 方法。install 方法将被作为 Vue 的参数调用。
294-
Install a Vue.js plugin. If the plugin is an Object, it must expose an `install` method. If it is a function itself, it will be treated as the install method. The install method will be called with Vue as the argument.
295269

296270
当 install 方法被同一个插件多次调用,插件将只会被安装一次。
297-
When this method is called on the same plugin multiple times, the plugin will be installed only once.
298271

299272
- **参考:** [插件](/guide/plugins.html)
300273

@@ -306,7 +279,6 @@ type: api
306279
- **用法:**
307280

308281
全局注册一个混合,影响注册之后所有创建的每个 Vue 实例。插件作者可以使用混合,向组件注入自定义逻辑。**不推荐在应用代码中使用**
309-
Apply a mixin globally, which affects every Vue instance created afterwards. This can be used by plugin authors to inject custom behavior into components. **Not recommended in application code**.
310282

311283
- **参考:** [全局混合](/guide/mixins.html#Global-Mixin)
312284

@@ -318,7 +290,6 @@ type: api
318290
- **用法:**
319291

320292
在render函数中编译模板字符串。**只在独立构建时有效**
321-
Compiles a template string into a render function. **Only available in the standalone build.**
322293

323294
``` js
324295
var res = Vue.compile('<div><span>{{ msg }}</span></div>')
@@ -345,38 +316,30 @@ type: api
345316
- **细节:**
346317

347318
Vue 实例的数据对象。Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化。**对象必须是普通对象**:浏览器 API 创建的原生对象,原型上的属性会被忽略。大概来说,data 应该只能是数据 - 不推荐观察拥有状态行为的对象。
348-
The data object for the Vue instance. Vue will recursively convert its properties into getter/setters to make it "reactive". **The object must be plain**: native objects such as browser API objects and prototype properties are ignored. A rule of thumb is that data should just be data - it is not recommended to observe objects with its own stateful behavior.
349319

350320
一旦观察过,不需要再次在数据对象上添加响应式属性。因此推荐在创建实例之前,就声明所有的根级响应式属性。
351-
Once observed, you can no longer add reactive properties to the root data object. It is therefore recommended to declare all root-level reactive properties upfront, before creating the instance.
352321

353322
实例创建之后,可以通过 `vm.$data` 访问原始数据对象。Vue 实例也代理了 data 对象上所有的属性,因此访问 `vm.a` 等价于访问 `vm.$data.a`
354-
After the instance is created, the original data object can be accessed as `vm.$data`. The Vue instance also proxies all the properties found on the data object, so `vm.a` will be equivalent to `vm.$data.a`.
355323

356324
`_``$` 开头的属性 **不会** 被 Vue 实例代理,因为它们可能和 Vue 内置的属性、 API 方法冲突。你可以使用例如 `vm.$data._property` 的方式访问这些属性。
357-
Properties that start with `_` or `$` will **not** be proxied on the Vue instance because they may conflict with Vue's internal properties and API methods. You will have to access them as `vm.$data._property`.
358325

359326
当一个组件被定义, `data` 必须声明为返回一个初始数据对象的函数,因为组件可能被用来创建多个实例。如果 `data` 仍然是一个普通对象,则所有的实例将**共享引用**同一个数据对象!通过提供 `data` 函数,每次创建一个新实例后,我们能够调用 `data` 函数,从而返回初始数据的一个全新副本数据对象。
360-
When defining a **component**, `data` must be declared as a function that returns the initial data object, because there will be many instances created using the same definition. If we still use a plain object for `data`, that same object will be **shared by reference** across all instances created! By providing a `data` function, every time a new instance is created, we can simply call it to return a fresh copy of the initial data.
361327

362328
可以通过将 `vm.$data` 传入 `JSON.parse(JSON.stringify(...))` 得到原始数据对象。
363-
If required, a deep clone of the original object can be obtained by passing `vm.$data` through `JSON.parse(JSON.stringify(...))`.
364329

365330
- **示例:**
366331

367332
``` js
368333
var data = { a: 1 }
369334

370335
// 直接创建一个实例
371-
// direct instance creation
372336
var vm = new Vue({
373337
data: data
374338
})
375339
vm.a // -> 1
376340
vm.$data === data // -> true
377341

378342
// Vue.extend() 中 data 必须是函数
379-
// must use function when in Vue.extend()
380343
var Component = Vue.extend({
381344
data: function () {
382345
return { a: 1 }
@@ -385,7 +348,6 @@ type: api
385348
```
386349

387350
<p class="tip">注意,__不应该对 `data` 属性使用箭头函数__ (例如`data: () => { return { a: this.myProp }}`)。理由是箭头函数绑定了父级作用域的上下文,所以 this 将不会按照期望指向 Vue 实例,`this.myProp` 将是 undefined。</p>
388-
<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>
389351

390352
- **参考:** [深入响应式原理](/guide/reactivity.html)
391353

@@ -395,26 +357,21 @@ type: api
395357

396358
- **细节:**
397359
props 可以是数组或对象,用于接收来自父组件的数据。props 可以是简单的数组,或者使用对象作为替代,对象允许配置高级选项,如类型检测、自定义校验和设置默认值。
398-
A list/hash of attributes that are exposed to accept data from the parent component. It has a simple Array-based syntax and an alternative Object-based syntax that allows advanced configurations such as type checking, custom validation and default values.
399360

400361
- **示例:**
401362

402363
``` js
403364
// 简单语法
404-
// simple syntax
405365
Vue.component('props-demo-simple', {
406366
props: ['size', 'myMessage']
407367
})
408368

409369
// 对象语法,提供校验
410-
// object syntax with validation
411370
Vue.component('props-demo-advanced', {
412371
props: {
413372
// 只检测类型
414-
// just type check
415373
height: Number,
416374
// 检测类型 + 其他验证
417-
// type check plus other validations
418375
age: {
419376
type: Number,
420377
default: 0,
@@ -434,12 +391,10 @@ type: api
434391
- **类型:** `{ [key: string]: any }`
435392

436393
- **限定:** 只用于 `new` 创建的实例中。
437-
- **Restriction:** only respected in instance creation via `new`.
438394

439395
- **细节:**
440396

441397
创建实例时传递 props。主要作用是方便测试。
442-
Pass props to an instance during its creation. This is primarily intended to make unit testing easier.
443398

444399
- **示例:**
445400

@@ -463,13 +418,10 @@ type: api
463418
- **细节:**
464419

465420
计算属性将被混入到 Vue 实例中。getter 和 setter 的 this 上下文自动地绑定为 Vue 实例。
466-
Computed properties to be mixed into the Vue instance. All getters and setters have their `this` context automatically bound to the Vue instance.
467421

468422
<p class="tip">注意,__不应该使用箭头函数来定义计算属性函数__ (例如 `aDouble: () => this.a * 2`)。理由是箭头函数绑定了父级作用域的上下文,所以 `this` 将不会按照期望指向 Vue 实例,`this.a` 将是 undefined。</p>
469-
<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>
470423

471424
计算属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算。
472-
Computed properties are cached, and only re-computed on reactive dependency changes.
473425

474426
- **示例:**
475427

@@ -478,12 +430,10 @@ type: api
478430
data: { a: 1 },
479431
computed: {
480432
// 仅读取,值只须为函数
481-
// get only, just need a function
482433
aDouble: function () {
483434
return this.a * 2
484435
},
485436
// 读取和设置
486-
// both get and set
487437
aPlus: {
488438
get: function () {
489439
return this.a + 1
@@ -510,10 +460,8 @@ type: api
510460
- **细节:**
511461

512462
methods 将被混入到 Vue 实例中。可以直接通过 VM 实例访问这些方法,或者在指令表达式中使用。方法中的 `this` 自动绑定为 Vue 实例。
513-
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.
514463

515464
<p class="tip">注意,__不应该使用箭头函数来定义 method 函数__ (例如 `plus: () => this.a++`)。理由是箭头函数绑定了父级作用域的上下文,所以 `this` 将不会按照期望指向 Vue 实例,`this.a` 将是 undefined。</p>
516-
<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>
517465

518466
- **示例:**
519467

@@ -539,7 +487,6 @@ type: api
539487
- **细节:**
540488

541489
一个对象,键是需要观察的表达式,值是对应回调函数。值也可以是方法名,或者包含选项的对象。Vue 实例将会在实例化时调用 `$watch()`,遍历 watch 对象的每一个属性。
542-
An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options. The Vue instance will call `$watch()` for each entry in the object at instantiation.
543490

544491
- **示例:**
545492

@@ -554,9 +501,9 @@ type: api
554501
a: function (val, oldVal) {
555502
console.log('new: %s, old: %s', val, oldVal)
556503
},
557-
// string method name
504+
// 方法名
558505
b: 'someMethod',
559-
// deep watcher
506+
// 深度 watcher
560507
c: {
561508
handler: function (val, oldVal) { /* ... */ },
562509
deep: true
@@ -567,7 +514,6 @@ type: api
567514
```
568515

569516
<p class="tip">注意,__不应该使用箭头函数来定义 watcher 函数__ (例如 `searchQuery: newValue => this.updateAutocomplete(newValue)`)。理由是箭头函数绑定了父级作用域的上下文,所以 `this` 将不会按照期望指向 Vue 实例,`this.updateAutocomplete` 将是 undefined。</p>
570-
<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>
571517

572518
- **参考:** [实例方法 - vm.$watch](#vm-watch)
573519

0 commit comments

Comments
 (0)