You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
+
<pclass="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
+
returnthis.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
+
<h3id="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
+
<h3id="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
+
<h3id="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
+
<h3id="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
+
<h3id="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.
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
+
newMyComponent().$mount('#app')
793
+
794
+
// the above is the same as:
795
+
newMyComponent({ el:'#app' })
796
+
797
+
// or, compile off-document and append afterwards:
This is an extremely common use case, but using JSX, there are a few problems that may not be immediately obvious:
80
75
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.
82
77
83
78
-__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.
84
79
@@ -114,7 +109,7 @@ It doesn't end there though. By embracing HTML rather than trying to reinvent it
114
109
115
110
#### Component-Scoped CSS
116
111
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.
118
113
119
114
Vue on the other hand, gives you full access to CSS within [single-file components](single-file-components.html):
120
115
@@ -186,7 +181,7 @@ That's why we offer a [Webpack template](https://github.com/vuejs-templates/webp
186
181
187
182
### Data binding
188
183
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.
0 commit comments