Note This document only captures the changeset upgrading from 0.10.x to 0.11.0. For changes in releases after 0.11.0, please refer to the Releases page.
Table of Contents
- Instantiation process
- New Scope Inheritance Model
- Instance Option changes
- Instance methods change
- Computed Properties API Change
- Directive API change
- Interpolation change
- Config API change
- Transition API change
- Events API change
- Two Way filters
- Block logic control
- Misc
Breaking
If no el
option is provided at instantiation, Vue will no longer auto-create an empty div for you. In this case, the instance is considered to be in "unmounted" state. Data will be observed, but no DOM compilation will happen until the new instance method $mount
has been explicitly called.
var vm = new Vue({ data: {a:1} }) // only observes the data
vm.$mount('#app') // actually compile the DOM
// in comparison, this will compile instantly just like before.
var vm = new Vue({ el: '#app', data: {a: 1} })
If $mount()
is called with no argument, an empty <div>
will then be created.
Breaking
In the previous version, nested Vue instances do not have prototypal inheritance of their data scope. Although you can access parent data properties in templates, you need to explicitly travel up the scope chain with this.$parent
in JavaScript code or use this.$get()
to get a property on the scope chain. The expression parser also needs to do a lot of dirty work to determine the correct scope the variables belong to.
In the new model, we provide a scope inheritance system similar to Angular, in which you can directly access properties that exist on parent scopes. The major difference is that setting a primitive value property on a child scope WILL affect that on the parent scope! Because all data properties in Vue are getter/setters, so setting a property with the same key as parent on a child will not cause the child scope to create a new property shadowing the parent one, but rather it will just invoke the parent's setter function. See the example here.
The result of this model is a much cleaner expression evaluation implementation. All expressions can simply be evaluated against the vm.
By default, all child components DO NOT inherit the parent scope. Only anonymous instances created in v-repeat
and v-if
inherit parent scope by default. This avoids accidentally falling through to parent properties when you didn't mean to. If you want your component to explicitly inherit parent scope, use the inherit:true
option.
-
Breaking
When used in component definitions and in
Vue.extend()
, theel
, anddata
options now only accept a function that returns the per-instance value. For example:var MyComponent = Vue.extend({ el: function () { var el = document.createElement('p') // you can initialize your element here. // you can even return a documentFragment to create // a block instance. el.className = 'content' return el }, data: function () { // similar to ReactComponent.getInitialState return { a: { b: 123 } } } })
-
paramAttributes
now behaves a little differently when the attribute name contains dashes:- If the attribute is a data attribute, the
data-
prefix will be auto stripped; - If the attribute still contains dashes, it will be camelized. The reason is because it's inconvenient to access top level properties containing dashes in templates: the expression
my-param
will be parsed as a minus expression unless you use the awkwardthis['my-param']
syntax.
This means a param attribute
data-hello
will be set on the vm asvm.hello
; Andmy-param
will be set asvm.myParam
. - If the attribute is a data attribute, the
-
When events are used extensively for cross-vm communication, the ready hook can get kinda messy. The new
events
option is similar to its Backbone equivalent, where you can declaratiely register a bunch of event listeners. You can also use it to register hook listeners.var vm = new Vue({ events: { 'hook:created': function () { console.log('created!') }, greeting: function (msg) { console.log(msg) }, // can also use a string for methods bye: 'sayGoodbye' }, methods: { sayGoodbye: function () { console.log('goodbye!') } } }) // -> created! vm.$emit('greeting', 'hi!') // -> hi! vm.$emit('bye') // -> goodbye!
-
Similar to the new
events
option, thewatch
option accepts an object of expression/callback pairs. The instance will automatically call$watch
for each entry in the object. You can also use a method name string instead of a callback. -
Default:
false
.Whether to inherit parent scope data. Set it to
true
if you want to create a component that inherits parent scope. By default, inside a component's template you won't be able to bind to data on parent scopes. When this is set totrue
, you will be able to:- bind to parent scope properties in the component template
- directly access parent properties on the component instance itself, via prototypal inheritance.
-
The
mixins
option accepts an array of mixin objects. These mixin objects can contain instance options just like normal instance objects, and they will be merged against the eventual options using the same merge logic inVue.extend
. e.g. If your mixin contains acreated
hook and the component itself also has one, both functions will be called.var mixin = { created: function () { console.log(2) } } var vm = new Vue({ created: function () { console.log(1) }, mixins: [mixin] }) // -> 1 // -> 2
-
This option, when used with
Vue.extend
, gives your returned constructor a more descriptive name rather than the genericVueComponent
. This makes debugging easier when you log instances in the console. For example:var SubClass = Vue.extend({ name: 'MyComponent' }) var instance = new SubClass() console.log(instance) // -> MyComponent { $el: ... }
When you use
Vue.component
, the component ID is automatically camelized and used as the constructor name, so"my-component"
will have a constructor name ofMyComponent
.This option is ignored as an instance option.
-
Breaking
-
parent
This option has been removed. To create a child instance that inherits parent scope, use
var child = parent.$addChild(options, [contructor])
. -
lazy
The
lazy
option is removed because this does not belong at the vm level. Users should be able to configure individualv-model
instances to be lazy or not.
The following are also removed, use
el
with a function instead:id
tagName
className
attributes
-
-
Breaking
-
In the past, you could do
this.something = 1
inside thecreated
hook to add observed data to the instance. Now the hook is called after the data observation, so if you wish to add additional data to the instance you should use the new$add
and$delete
API methods. -
The new
ready
hook now is only fired after the instance is compiled and inserted into the document for the first time. For a equivalence of the oldready
hook, use the newcompiled
hook. -
This new hook is introduced to accompany the separation of instantiation and DOM mounting. It is called right before the DOM compilation starts and
this.$el
is available, so you can do some pre-processing on the element here. -
The
compiled
hook indicates the element has been fully compiled based on initial data. However this doesn't indicate if the element has been inserted into the DOM yet. This is essentially the oldready
hook. -
Additionally, if there is a leaving transition,
beforeDestroy
is called before the transition starts, anddestroyed
will be called after the transition has finished (right afterdetached
).
-
-
-
Expression watching
vm.$watch
can now accept an expression:vm.$watch('a + b', function (newVal, oldVal) { // do something })
-
Deep watching
(Breaking) A change from 0.11 is that
$watch
now by default only fires when the identity of the watched value changes. If you want the watcher to also fire the callback when a nested value changes, pass in the third optionaldeep
argument:vm.$watch('someObject', callback, true) vm.someObject.nestedValue = 123 // callback is fired
-
Immediate invocation
By default the callback only fires when the value changes. If you want it to be called immediately with the initial value, use the fourth optional
immediate
argument:vm.$watch('a', callback, false, true) // callback is fired immediately with current value of `a`
-
Unwatching
(Breaking)
$unwatch
has been removed.$watch
now also returns an unregister function:var unwatch = vm.$watch('a', cb) // later, teardown the watcher unwatch()
-
-
vm.$get
now accepts expressions:var value = vm.$get('a + b')
-
-
vm.$add
andvm.$delete
Explicitly add/remove properties from the ViewModel. Observed objects are augmented with these two methods too. Use these only when needed - the best practice is to define all your data fields at instantiation, even with a
null
value. -
vm.$eval
Evaluate an expression that can also include filters.
var value = vm.$eval('msg | uppercase')
-
vm.$interpolate
Evalutate a piece of template string.
var markup = vm.$interpolate('<p>{{msg | uppercase}}</p>')
-
vm.$log
Log the current instance data as a plain object, which is more console-inspectable than a bunch of getter/setters. Also accepts an optional key.
vm.$log() // logs entire ViewModel data vm.$log('item') // logs vm.item
-
vm.$compile
Partially compile a piece of DOM (Element or DocumentFragment). Returns a "decompile" function that tearsdown the directives created during the process. Note the decompile function does not remove the DOM. This method is exposed primarily for writing advanced custom directives.
-
Breaking
$get
and $set
is now simply get
and set
:
computed: {
fullName: {
get: function () {},
set: function () {}
}
}
-
Now each directive can optionally have a
priority
value which determines the order it gets compiled among all directives on the same element. Priorities for some built-in directives will be available in the API reference after 0.11 is released. -
Literal directives can now also be dynamic via bindings like this:
<div v-component="{{test}}"></div>
When
test
changes, the component used will change! This essentially replaces the oldv-view
directive.When authoring literal directives, you can now provide an
update()
function if you wish to handle it dynamically. If noupdate()
is provided the directive will be treated as a static literal and only evaluated once.Note that
v-component
andv-partial
are the only directives that support this. -
Some built-in directives now checks for additional attribute params to trigger special behavior.
-
v-model
BREAKING
v-model
no longer works on arbitrary elements withcontenteditable
. It is now recommended to wrap a library that specifically deals withcontenteditable
inside a custom directive.v-model
now will checklazy
attribute for lazy model update, and will checknumber
attribute to know if it needs to convert the value into Numbers before writing back to the model.When used on a
<select>
element,v-model
will check for anoptions
attribute, which should be an keypath/expression that points to an Array to use as its options. The Array can contain plain strings, or contain objects.The object can be in the format of
{text:'', value:''}
. This allows you to have the option text displayed differently from its underlying value:[ { text: 'A', value: 'a' }, { text: 'B', value: 'b' } ]
Will render:
<select> <option value="a">A</option> <option value="b">B</option> </select>
Alternatively, the object can be in the format of
{ label:'', options:[...] }
. In this case it will be rendered as an<optgroup>
:[ { label: 'A', options: ['a', 'b']}, { label: 'B', options: ['c', 'd']} ]
Will render:
<select> <optgroup label="A"> <option value="a">a</option> <option value="b">b</option> </optgroup> <optgroup label="B"> <option value="c">c</option> <option value="d">d</option> </optgroup> </select>
-
v-component
When used as a dynamic component, it will check for the
keep-alive
attribute. Whenkeep-alive
is present, already instantiated components will be cached. This is useful when you have large, nested view components and want to maintain the state when switching views. -
v-repeat
One of the questions I've asked about is how
v-repeat
does the array diffing and what happens if we swap the array with a fresh array grabbed from an API end point. In 0.10 because the objects are different, all instances have to been re-created. In 0.11 we introduce thetrackby
attribute param. If each of your data objects in the array has a unique id, we can use that id to reuse existing instances when the array is swapped.For example, if we have the data in the following format:
items: [ { _id: 1, ... }, { _id: 2, ... }, { _id: 3, ... } ]
In your template you can do:
<li v-repeat="items" trackby="_id">...</li>
Later on when you swap
items
with a different array, even if the objects it contains are new, as long as they have the same trackby id we can still efficiently reuse existing instances.
-
-
In 0.10 and earlier,
v-with
creates a two-way binding between the parent and child instance. In 0.11, it no longer creates a two-way binding but rather facilitates a unidirectional data flow from parent to child.For example:
<div v-component="test" v-with="childKey:parentKey">{{childKey}}</div>
Here when you do
this.a = 123
in the child, the child's view will update, but the parent's scope will remain unaffected. Whenparent.parentKey
changes again, it will overwritechild.childKey
. -
Similar to
v-ref
, but instead stores a reference to a DOM Node invm.$$
. For the reasoning behind the addition see this thread. -
This option indicates the directive is two-way and may write back to the model. Allows the use of
this.set(value)
inside directive functions. -
This option indicates the directive accepts inline statements like
v-on
does:<a v-on="click: a++"></a>
The statement will be wrapped up as a function and passed as the argument to the directive's
update
function.
-
(Breaking) Text bindings will no longer automatically stringify objects. Use the new
json
filter which gives more flexibility in formatting. -
One time interpolations do not need to set up watchers and can improve initial rendering performance. If you know something's not going to change, make sure to use this new feature. Example:
<span>{{* hello }}</span>
Breaking
Instead of the old Vue.config()
with a heavily overloaded API, the config object is now available globally as Vue.config
, and you can simply change its properties:
// old
// Vue.config('debug', true)
// new
Vue.config.debug = true
-
Config prefix now should include the hyphen: so the default is now
v-
and if you want to change it make sure to include the hyphen as well. e.g.Vue.config.prefix = "data-"
. -
In the old version the interpolation delimiters are limited to the same base character (i.e.
['{','}']
translates into{{}}
for text and{{{}}}
for HTML). Now you can set them to whatever you like (*almost), and to indicate HTML interpolation, simply wrap the tag with one extra outer most character on each end. Example:Vue.config.delimiters = ['(%', '%)'] // tags now are (% %) for text // and ((% %)) for HTML
- Note you still cannot use
<
or>
in delimiters because Vue uses DOM-based templating.
- Note you still cannot use
-
By default, Vue.js alters the
__proto__
of observed Arrays when available for faster method interception/augmentation. This would only cause issue in the rare case when you are observing a subclass of the native Array. In that case, you can setVue.config.proto = false
to prohibit this behavior. -
By default Vue.js uses batched async updates for watchers and DOM updates. This strategy ensures minimal calls to directive and watcher functions, but in some situations also makes things harder to reason about. It is now possible to force synchronous updates by setting
Vue.config.async = false
.
Breaking
- no more distinctions between
v-transition
,v-animation
orv-effect
; - no more configuring enter/leave classes in
Vue.config
; Vue.effect
has been replaced withVue.transition
, theeffects
option has also been replaced bytransitions
.
With v-transition="my-transition"
, Vue will:
-
Try to find a transition definition object registered either through
Vue.transition(id, def)
or passed in with thetransitions
option, with the id"my-transition"
. If it finds it, it will use that definition object to perform the custom JavaScript based transition. -
If no custom JavaScript transition is found, it will automatically sniff whether the target element has CSS transitions or CSS animations applied, and add/remove the classes as before.
-
If no transitions/animations are detected, the DOM manipulation is executed immediately instead of hung up waiting for an event.
-
Now more similar to Angular, and all hooks are called with
this
set to the vm owning the transitioned element:Vue.transition('fade', { beforeEnter: function (el) { // a synchronous function called right before the // element is inserted into the document. // you can do some pre-styling here to avoid FOC. }, enter: function (el, done) { // element is already inserted into the DOM // call done when animation finishes. $(el) .css('opacity', 0) .animate({ opacity: 1 }, 1000, done) // optionally return a "cancel" function // to clean up if the animation is cancelled return function () { $(el).stop() } }, leave: function (el, done) { // same as enter $(el) .animate({ opacity: 0 }, 1000, done) return function () { $(el).stop() } } })
Now if an event handler return false
, it will stop event propagation for $dispatch
and stop broadcasting to children for $broadcast
.
var a = new Vue()
var b = new Vue({
parent: a
})
var c = new Vue({
parent: b
})
a.$on('test', function () {
console.log('a')
})
b.$on('test', function () {
console.log('b')
return false
})
c.$on('test', function () {
console.log('c')
})
c.$dispatch('test')
// -> 'c'
// -> 'b'
If a filter is defined as a function, it is treated as a read filter by default - i.e. it is applied when data is read from the model and applied to the DOM. You can now specify write filters as well, which are applied when writing to the model, triggered by user input. Write filters are only triggered on two-way bindings like v-model
.
Vue.filter('format', {
read: function (val) {
return val + '!'
},
write: function (val, oldVal) {
return val.match(/ok/) ? val : oldVal
}
})
One limitation of flow control direcitves like v-repeat
and v-if
is that they can only be used on a single element. Now you can use them to manage a block of content by using them on a <template>
element that wraps the content you want managed:
items: [
{
title: 'title-1',
subtitle: 'subtitle-1',
content: 'content-1'
},
{
title: 'title-2',
subtitle: 'subtitle-2',
content: 'content-2'
}
]
<template v-repeat="item:items">
<h2>{{item.title}}</h2>
<p>{{item.subtitle}}</p>
<p>{{item.content}}</p>
</template>
Rendered result:
<!--v-block-start-->
<h2>title-1</h2>
<p>subtitle-1</p>
<p>content-1</p>
<!--v-block-end-->
<!--v-block-start-->
<h2>title-2</h2>
<p>subtitle-2</p>
<p>content-2</p>
<!--v-block-end-->
Additionally, you can also use v-partial
with <template>
for a block partial:
<template v-partial="abc"></template>
Which is the equivalance of {{>abc}}
. However, the <template>
syntax allows you to bind a dynamic block partial:
<template v-partial="{{partialId}}"></template>
-
$destroy()
now by default leaves$el
intact. If you want to remove it (and trigger transitions), call$destroy(true)
. -
When there are inline values on input elements bound with
v-model
, e.g.<input value="hi" v-model="msg">
, the inline value will be used as the inital value. If the vm comes with default data, it will be overwritten by the inline value. Same forselected
attribute on<option>
elements.