Skip to content

Commit 5f095e1

Browse files
authored
Merge pull request #3 from vuejs/2.0
2.0 - 8-15 更新
2 parents e9f0d0f + a56d0eb commit 5f095e1

File tree

3 files changed

+334
-18
lines changed

3 files changed

+334
-18
lines changed

src/api/index.md

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,324 @@ type: api
499499
```
500500

501501
- **See also:** [Instance Methods - vm.$watch](#vm-watch)
502+
503+
## Instance Properties
504+
505+
### vm.$data
506+
507+
- **Type:** `Object`
508+
509+
- **Details:**
510+
511+
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.
512+
513+
### vm.$el
514+
515+
- **Type:** `HTMLElement`
516+
517+
- **Read only**
518+
519+
- **Details:**
520+
521+
The DOM element that the Vue instance is managing. `vm.$el` will return a DOM element that created with the Vue instance.
522+
523+
### vm.$options
524+
525+
- **Type:** `Object`
526+
527+
- **Read only**
528+
529+
- **Details:**
530+
531+
The instantiation options used for the current Vue instance. This is useful when you want to include custom properties in the options:
532+
533+
``` js
534+
new Vue({
535+
customOption: 'foo',
536+
created: function () {
537+
console.log(this.$options.customOption) // -> 'foo'
538+
}
539+
})
540+
```
541+
542+
### vm.$parent
543+
544+
- **Type:** `Vue instance`
545+
546+
- **Read only**
547+
548+
- **Details:**
549+
550+
The parent instance, if the current instance has one.
551+
552+
### vm.$root
553+
554+
- **Type:** `Vue instance`
555+
556+
- **Read only**
557+
558+
- **Details:**
559+
560+
The root Vue instance of the current component tree. If the current instance has no parents this value will be itself.
561+
562+
### vm.$children
563+
564+
- **Type:** `Array<Vue instance>`
565+
566+
- **Read only**
567+
568+
- **Details:**
569+
570+
The direct child components of the current instance.
571+
572+
### vm.$refs
573+
574+
- **Type:** `Object`
575+
576+
- **Read only**
577+
578+
- **Details:**
579+
580+
An object that holds child components that have `ref` registered.
581+
582+
- **See also:**
583+
- [Child-Component-Reference](/guide/components.html#Child-Component-Reference)
584+
- !!TODO: [ref](#ref).
585+
586+
### vm.$isServer
587+
588+
- **Type:** `Boolean`
589+
590+
- **Read only**
591+
592+
- **Details:**
593+
594+
Whether the current Vue instance is running on the server-side.
595+
596+
- **See also:**
597+
- !!TODO: [Server-Side Rendering](/guide/ssr.html).
598+
599+
## Instance Methods / Data
600+
601+
<h3 id="vm-watch">vm.$watch( expOrFn, callback, [options] )</h3>
602+
603+
- **Arguments:**
604+
- `{String | Function} expOrFn`
605+
- `{Function} callback`
606+
- `{Object} [options]`
607+
- `{Boolean} deep`
608+
- `{Boolean} immediate`
609+
610+
- **Returns:** `{Function} unwatch`
611+
612+
- **Usage:**
613+
614+
Watch an expression or a computed function on the Vue instance for changes. The callback gets called with the new value and the old value. The expression can be a single keypath or any valid binding expressions.
615+
616+
<p class="tip">Note: when mutating (rather than replacing) an Object or an Array, the old value will be the same as new value because they reference the same Object/Array. Vue doesn't keep a copy of the pre-mutate value.</p>
617+
618+
- **Example:**
619+
620+
``` js
621+
// keypath
622+
vm.$watch('a.b.c', function (newVal, oldVal) {
623+
// do something
624+
})
625+
626+
// expression
627+
vm.$watch('a + b', function (newVal, oldVal) {
628+
// do something
629+
})
630+
631+
// function
632+
vm.$watch(
633+
function () {
634+
return this.a + this.b
635+
},
636+
function (newVal, oldVal) {
637+
// do something
638+
}
639+
)
640+
```
641+
642+
`vm.$watch` returns an unwatch function that stops firing the callback:
643+
644+
``` js
645+
var unwatch = vm.$watch('a', cb)
646+
// later, teardown the watcher
647+
unwatch()
648+
```
649+
650+
- **Option: deep**
651+
652+
To also detect nested value changes inside Objects, you need to pass in `deep: true` in the options argument. Note that you don't need to do so to listen for Array mutations.
653+
654+
``` js
655+
vm.$watch('someObject', callback, {
656+
deep: true
657+
})
658+
vm.someObject.nestedValue = 123
659+
// callback is fired
660+
```
661+
662+
- **Option: immediate**
663+
664+
Passing in `immediate: true` in the option will trigger the callback immediately with the current value of the expression:
665+
666+
``` js
667+
vm.$watch('a', callback, {
668+
immediate: true
669+
})
670+
// callback is fired immediately with current value of `a`
671+
```
672+
## Instance Methods / Events
673+
674+
<h3 id="vm-on">vm.$on( event, callback )</h3>
675+
676+
- **Arguments:**
677+
- `{String} event`
678+
- `{Function} callback`
679+
680+
- **Usage:**
681+
682+
Listen for a custom event on the current vm. Events can be triggered by `vm.$emit`. The callback will receive all the additional arguments passed into these event-triggering methods.
683+
684+
- **Example:**
685+
686+
``` js
687+
vm.$on('test', function (msg) {
688+
console.log(msg)
689+
})
690+
vm.$emit('test', 'hi')
691+
// -> "hi"
692+
```
693+
694+
<h3 id="vm-once">vm.$once( event, callback )</h3>
695+
696+
- **Arguments:**
697+
- `{String} event`
698+
- `{Function} callback`
699+
700+
- **Usage:**
701+
702+
Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.
703+
704+
<h3 id="vm-off">vm.$off( [event, callback] )</h3>
705+
706+
- **Arguments:**
707+
- `{String} [event]`
708+
- `{Function} [callback]`
709+
710+
- **Usage:**
711+
712+
Remove event listener(s).
713+
714+
- If no arguments are provided, remove all event listeners;
715+
716+
- If only the event is provided, remove all listeners for that event;
717+
718+
- If both event and callback are given, remove the listener for that specific callback only.
719+
720+
<h3 id="vm-emit">vm.$emit( event, [...args] )</h3>
721+
722+
- **Arguments:**
723+
- `{String} event`
724+
- `[...args]`
725+
726+
Trigger an event on the current instance. Any additional arguments will be passed into the listener's callback function.
727+
728+
## Instance Methods / DOM
729+
730+
<h3 id="vm-nextTick">vm.$nextTick( callback )</h3>
731+
732+
- **Arguments:**
733+
- `{Function} [callback]`
734+
735+
- **Usage:**
736+
737+
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. This is the same as the global `Vue.nextTick`, except that the callback's `this` context is automatically bound to the instance calling this method.
738+
739+
- **Example:**
740+
741+
``` js
742+
new Vue({
743+
// ...
744+
methods: {
745+
// ...
746+
example: function () {
747+
// modify data
748+
this.message = 'changed'
749+
// DOM is not updated yet
750+
this.$nextTick(function () {
751+
// DOM is now updated
752+
// `this` is bound to the current instance
753+
this.doSomethingElse()
754+
})
755+
}
756+
}
757+
})
758+
```
759+
760+
- **See also:**
761+
- [Vue.nextTick](#Vue-nextTick)
762+
-!!TODO: [Async Update Queue](/guide/reactivity.html#Async-Update-Queue)
763+
764+
## Instance Methods / Lifecycle
765+
766+
<h3 id="vm-mount">vm.$mount( [elementOrSelector], [hydrating] )</h3>
767+
768+
- **Arguments:**
769+
- `{Element | String} [elementOrSelector]`
770+
- `{Boolean} [hydrating]`
771+
772+
- **Returns:** `vm` - the instance itself
773+
774+
- **Usage:**
775+
776+
If a Vue instance didn't receive the `el` option at instantiation, it will be in "unmounted" state, without an associated DOM element. `vm.$mount()` can be used to manually start the mounting/compilation of an unmounted Vue instance.
777+
778+
If `elementOrSelector` argument is not provided, the element is managed in Vue instance will be created as an out-of-document DOM element, and you will have to use DOM API to insert it into the document yourself.
779+
780+
If `hydrating` argument is provided as `true`, in the rendering process of this method, run the DOM elements hydration.
781+
782+
The method returns the instance itself so you can chain other instance methods after it.
783+
784+
- **Example:**
785+
786+
``` js
787+
var MyComponent = Vue.extend({
788+
template: '<div>Hello!</div>'
789+
})
790+
791+
// create and mount to #app (will replace #app)
792+
new MyComponent().$mount('#app')
793+
794+
// the above is the same as:
795+
new MyComponent({ el: '#app' })
796+
797+
// or, compile off-document and append afterwards:
798+
var component = new MyComponent().$mount()
799+
document.getElementById('app').appendChild(vm.$el)
800+
```
801+
802+
- **See also:**
803+
- [Lifecycle Diagram](/guide/instance.html#Lifecycle-Diagram)
804+
- !!TODO: [Server-Side Rendering](/guide/ssr.html)
805+
806+
<h3 id="vm-forceUpdate">vm.$forceUpdate( )</h3>
807+
808+
- **Usage:**
809+
810+
The Vue instance will be forced into a “digest cycle”, during which all its watchers are re-evaluated.
811+
812+
<p class="tip">Note: This method have an influence on your application performance degradation. The excessive call is no recommended.</p>
813+
814+
<h3 id="vm-destroy">vm.$destroy( )</h3>
815+
816+
- **Usage:**
817+
818+
Completely destroy a vm. Clean up its connections with other existing vms, unbind all its directives, turn off all event listeners.
819+
820+
Triggers the `beforeDestroy` and `destroyed` hooks.
821+
822+
- **See also:** [Lifecycle Diagram](/guide/instance.html#Lifecycle-Diagram)

src/guide/comparison.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,21 @@ render () {
5959
const { items } = this.props
6060

6161
return (
62-
<div className='list-container'>{
63-
items.length
64-
? <ul>{
65-
items.map(item => {
66-
return (
67-
<li key={item.id}>
68-
{ item.name }
69-
</li>
70-
)
71-
})
72-
}</ul>
62+
<div className='list-container'>
63+
{items.length
64+
? <ul>
65+
{items.map(item => <li key={item.id}>{item.name}</li>)}
66+
</ul>
7367
: <p>No items found.</p>
74-
}</div>
68+
}
69+
</div>
7570
)
7671
}
7772
```
7873

7974
This is an extremely common use case, but using JSX, there are a few problems that may not be immediately obvious:
8075

81-
- __Syntax Noise__: All the curly braces add a lot of visual noise, but what's worse is that while writing it, you have to frequently make decisions about how to minimize their visual impact, slowing development.
76+
- __Syntax Noise__: All the curly braces add visual noise, but what's worse is that while writing it, you have to frequently make decisions about how to minimize their visual impact, slowing development.
8277

8378
- __Cajoling JavaScript into Expressions__: The example uses a ternary rather than an if statement here, which arguably makes the code less readable, but is still possibly the best of bad options. The only alternatives include hoisting this logic out of its context or wrapping an if statement in an immediately-invoked function expression. Do expressions will help a little when they're a more stable feature, but they're currently at stage 0.
8479

@@ -114,7 +109,7 @@ It doesn't end there though. By embracing HTML rather than trying to reinvent it
114109

115110
#### Component-Scoped CSS
116111

117-
Unless you're OK spreading components out over multiple files, there simply isn't a good option for scoping CSS in React. Very basic CSS works fine, but common features such as hover states, media queries, and pseudo-selectors all either require heavy dependencies to reinvent what CSS already does - or they simply don't work. And no matter what you end up using in the end, it'll have involved a lot of research before you can even get a simple hover state to work.
112+
Unless you're OK spreading components out over multiple files (for example with [CSS Modules](https://github.com/gajus/react-css-modules)), it's difficult to find a good solution for scoping CSS in React. Very basic CSS works fine, but common features such as hover states, media queries, and pseudo-selectors all either require heavy dependencies to reinvent what CSS already does - or they simply don't work. And no matter what you end up using in the end, it'll have involved a lot of research before you can even get a simple hover state to work.
118113

119114
Vue on the other hand, gives you full access to CSS within [single-file components](single-file-components.html):
120115

@@ -186,7 +181,7 @@ That's why we offer a [Webpack template](https://github.com/vuejs-templates/webp
186181

187182
### Data binding
188183

189-
Angular 1 uses two-way binding between scopes, while Vue enforces a one-way data flow between components. The makes the flow of data easier to reason about in non-trivial applications.
184+
Angular 1 uses two-way binding between scopes, while Vue enforces a one-way data flow between components. This makes the flow of data easier to reason about in non-trivial applications.
190185

191186
### Directives vs Components
192187

0 commit comments

Comments
 (0)