Skip to content

Commit a581bf7

Browse files
committed
computed, methods, and watch reviewed
1 parent 4dd36de commit a581bf7

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

src/api/index.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,11 @@ type: api
364364
// type check plus other validations
365365
name: {
366366
type: String,
367+
default: 0,
367368
required: true,
368-
// warn if not two way bound
369-
twoWay: true
369+
validator: function (value) {
370+
return value > 0
371+
}
370372
}
371373
}
372374
})
@@ -400,3 +402,100 @@ type: api
400402
}
401403
})
402404
```
405+
406+
### computed
407+
408+
- **Type:** `Object`
409+
410+
- **Details:**
411+
412+
Computed properties to be mixed into the Vue instance. All getters and setters have their `this` context automatically bound to the Vue instance.
413+
414+
- **Example:**
415+
416+
```js
417+
var vm = new Vue({
418+
data: { a: 1 },
419+
computed: {
420+
// get only, just need a function
421+
aDouble: function () {
422+
return this.a * 2
423+
},
424+
// both get and set
425+
aPlus: {
426+
get: function () {
427+
return this.a + 1
428+
},
429+
set: function (v) {
430+
this.a = v - 1
431+
}
432+
}
433+
}
434+
})
435+
vm.aPlus // -> 2
436+
vm.aPlus = 3
437+
vm.a // -> 2
438+
vm.aDouble // -> 4
439+
```
440+
441+
- **See also:**
442+
- [Computed Properties](/guide/computed.html)
443+
- [Reactivity in Depth: Inside Computed Properties](/guide/reactivity.html#Inside-Computed-Properties)
444+
445+
### methods
446+
447+
- **Type:** `Object`
448+
449+
- **Details:**
450+
451+
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.
452+
453+
- **Example:**
454+
455+
```js
456+
var vm = new Vue({
457+
data: { a: 1 },
458+
methods: {
459+
plus: function () {
460+
this.a++
461+
}
462+
}
463+
})
464+
vm.plus()
465+
vm.a // 2
466+
```
467+
468+
- **See also:** [Methods and Event Handling](/guide/events.html)
469+
470+
### watch
471+
472+
- **Type:** `Object`
473+
474+
- **Details:**
475+
476+
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.
477+
478+
- **Example:**
479+
480+
``` js
481+
var vm = new Vue({
482+
data: {
483+
a: 1
484+
},
485+
watch: {
486+
'a': function (val, oldVal) {
487+
console.log('new: %s, old: %s', val, oldVal)
488+
},
489+
// string method name
490+
'b': 'someMethod',
491+
// deep watcher
492+
'c': {
493+
handler: function (val, oldVal) { /* ... */ },
494+
deep: true
495+
}
496+
}
497+
})
498+
vm.a = 2 // -> new: 2, old: 1
499+
```
500+
501+
- **See also:** [Instance Methods - vm.$watch](#vm-watch)

0 commit comments

Comments
 (0)