Closed yyx990803 closed 7 years ago
What about providing an interface to create custom v-model modifiers as a way to replace 2 way filters? It seems they are already being used to parse user input (for example v-model.trim
). If the parsing/ formatting is simple and independent from a specific data property, using a modifier would allow the parsing/formatting to be reused with much less boilerplate than by setting a computed property for each individual data property, or creating a new component for each input type where we want the parsing/formatting to be applied.
Hi, I'm currently working on a plugin to support gettext
in Vue.js 1.0
and I have a directive that is using vm.$interpolate
.
I'm wondering how to migrate my code to 2.0
since:
vm.$interpolate
will be deprecated2.0
Or is there a better approach than a directive?
import languages from 'src/plugins/translate/languages'
import translateUtils from 'src/plugins/translate/utils'
const translateDirective = {
terminal: true,
params: ['translateN', 'translatePlural'],
paramWatchers: {
translateN: function () {
this.translate()
},
},
isPlural: function () {
return 'translateN' in this.params && 'translatePlural' in this.params
},
bind: function () {
this.boundTranslate = this.translate.bind(this)
this.msgid = this.el.innerHTML.trim()
this.translate()
languages.eventEmitter.on(languages.languageChangedEventName, this.boundTranslate)
},
unbind: function () {
languages.eventEmitter.removeListener(languages.languageChangedEventName, this.boundTranslate)
},
translate: function () {
let n = this.isPlural() ? parseInt(this.params.translateN) : 1
let translation = translateUtils.getTranslation(this.msgid, n)
this.el.innerHTML = this.vm.$interpolate(translation)
},
}
export default translateDirective
Just throwing some short words since I'm new at Vue, just to say that I'm happy to see the reduction of the API in general or the under-the-hood helpers. JavaScript is really powerful already, and with computed properties plus other reactive features of the framework almost everything can be achieved.
Kudos for this next release! 🎆
@kemar I'm not that familiar with gettext, but I would simply extend Vue.prototype with a $translate method, and then do
{{ $translate('some.Key.path') }}
Has the ability to register assets with array syntax been removed from 2.0? Not working on alpha, just wondering if it's intentional or not. i.e:
components: [compA, compB, compC]
I know ES6 has shorthand which looks similar, but there are some scenarios where the array syntax was useful.
I see you mention rendering to native interfaces on mobile
with weex
and I was wondering how easy it would be to make vue and Nativescript talk.
Something more along the lines of Nativescript for Vue would be either Weex, which you mentioned, or Quasar.
Scott
If there is no longer dispatch
or broadcast
how will a generic child component inform its parent of an event/change? This doesn't seem to fit the pattern of a global bus or vuex. Use case we use now is a range slider for search filters. The range slider component is generic and is a child of several different search filters. We currently use dispatch
when a range slider has finished sliding, then the parent knows to trigger a search based on the change.
@jrenton inline listeners <child @some-event="parentHandler">
Great job guys.
In my point of view, all the changes say that the best approach is to create your component tree based on only a "one way flow", that is so much more simple and easy to debug and maintain.
Without this, your data truthness would be inverse proportional off how far you are from the top most component.
Just want to say that:
render (h) {
return (
<div>
{this.things.map(thing => <Thing thing={thing}></Thing>)}
</div>
);
Makes me happy
Is it expected that more will be added to this list before the release of 2.0? Just curious being as the issue is still open.
@zephraph Yes, we are continuously updating the list as we make updates to the API. 😃 Nothing huge so far, but occasional breaking changes from previous alphas.
I have a case, I used event dispatching in the past and where I'm stuck with vuex: The parent component has a list of child components and the child was dispatching an event when it's value changed so the parent was able to do something as a reaction of that change. Now I tried to have an array of the child values in the vuex store. The thing is, how does the child component know in the getter, and in the action, what element of that array it needs to update. As far as I see, vuex does not provide functionality to dynamically get or trigger a mutuation of a value, or am I wrong about that? What would be the best way to handle this case without event dispatching?
With $broadcast
being removed, how would you tell a direct child to do something when a particular thing happens? This would be a scenario where no data has actually changed, so reactive props don't seem to fit.
I could use a prop and pass down a timestamp or some random data and watch that prop in the child but that seems strange. A global event bus would require generation of unique IDs so the the child is only reacting to events from it's parent and not any other instance of the parent component.
There's $emit
in a child which a parent can listen to using an inline listener, is there anything for the other way round?
I could pass down an instance of an emitter via a prop, then emmiter.on
in the child, does that sound terrible?
@gwildu actions can take arguments, so you can pass item id along with the action. And instead of making the item component fetch the corresponding item from the store, fetch the list in the parent, and pass item data to the item component with a prop.
// Vuex store
state: {
items: [],
},
mutations: {
ITEM_REMOVED: function(state, id) {
var io = state.items.findIndex(item => item.id === id);
state.items.splice(io, 1);
},
},
// within the parent component
vuex: {
getters: {
items: state => state.items,
},
actions: {
removeItem(store, id) {
store.dispatch('ITEM_REMOVED', id);
},
},
},
<!-- within the parent template -->
<item v-for="item in item"
:item-data="item.data"
@removed="removeItem(item.id)"
>
</item>
<!-- within the child template -->
<button @click="$emit('removed')">Remove</button>
If your items do not have a locally unique ids, you can generate one when the item is created or received from the API. Most of the time cuid is good enough for that.
@fergaldoyle due to the parent always knowing it's children, you can put v-ref:some-child
on the child to get the reference to the child's vm, and then either $emit
on it, or just call a method directly with this.$refs.someChild.<methodName>(...)
.
However I would advise rethinking the architecture in that case, because events flowing down make the component really hard to reason about.
I was playing around with vuejs 2 and I noticed for Snabbdom if you pass
h('div', {style: {color: '#000'}}, [
h('h1', 'Headline'),
h('p', 'A paragraph'),
]);
To the render function you get
<div style="color:#000;"><h1>Headline</h1><p>A paragraph</p></div>
But in vuejs you get
<div style="color:#000;"><h1></h1><p></p></div>
Is there a way to modify the text content (inside <h1>
)?
@dubcanada why not just pass those as children?
Right that would make sense. Thanks
Hi. I have some question about transition system in Vue 2.0 or rather a proposal because I don't see it being in the plans for Vue 2.0. In Vue 1.0 i often encountered the need to detect when some transition/animation i have set up would end. Now I do this by using setTimeout, but this is very hacky and ugly way, we can all agree. So my question is, will be in Vue 2.0 some way to detect the end of CSS transition when we use transition combined with v-show/v-if, possibly via event?
<my-comp v-show="isVisible" @transition-end="onTransitionEnd" transition></my-comp>
i would be very happy to see something like this in next Vue release :) thanks for hearing me out
@sqal you can do that with transition hooks: https://jsfiddle.net/simplesmiler/Lrk9sxjf/97/
@dubcanada it will be supported in the next release (omitting data when creating element)
Thanks @fergaldoyle and @simplesmiler for your hint to emit.
I was not aware, the parent is able to listen to events that were emitted by the child. Of course it makes more sense to listen to that non-bubbling event then.
Hi all. A bit of background: we are working with webgl and I would like to do some interfaces on a 3D surface. This means that we need to render an interface to for example a canvas and then convert the content of the canvas to a texture.
I have been working with Angular, React and Vue and to me Vue just makes the most sense! While reading about React I came across the react-canvas project. The interesting thing is that instead of transforming the virtual DOM into real DOM nodes, they draw it to a canvas.
Because Vue 2.0 is also using a virtual DOM I was wondering if something like this can be done as well?
Hello,
Just some clarification of the removing of .sync
and what a generic workflow for handling props on a generic component might look like.
so, going from
<component :value.sync="some.value"></component>
to
<component :value="some.value" @update="updateSomeValue"></component>
What is the recommended way of tracking the prop value
?
In the most basic case, it seems to be
props: ['value'],
computed: {
_value: {
get(){
return this.value;
},
set(newVal) {
this.$emit('update', newVal);
return newVal;
}
}
}
But, surely this relies on the parent component returning that value to the prop, so when the component gets it again, it reflects the most recent change...
Does that mean we now need to do something like this:
props: ['value'],
data() {
return {
_val: this.value
}
},
watch: {
value(newVal) {
this._val = newVal;
}
},
computed: {
_value: {
get(){
return this._val;
},
set(newVal) {
this._val = newVal
this.$emit('update', newVal);
}
}
}
This seems like a lot of boilerplate to handle the passing in (and changing) of a value, notifying the parent that the value has changed, and tracking the change internally, in case the change isn't propagated back by the parent.
Or am I missing a bit of vue magic reactivity here?
Also, there is a chance that if there are a lot of props to handle, this could get quite complicated.
I could almost see myself working out a wrapper-component, where children access this.$parent.value
in order to mutate directly, and the wrapper-component just handling the props/computed/watches
@Towerful what exactly do you mean by "tracking the value"? And why do you want setter style (this._value = newValue
) instead of explicit this.$emit('value-updated', newValue)
?
The power of one-way flow is that the parent can decide to not apply the change requested by the child, or can mark the child as "on hold" and apply the change later (e.g. after checking back with the server).
@simplesmiler using a computed property allows you to bind it in the template, no? so you can use v-model
.
And having the setter & getter contained in 1 place makes it easy to go and see the functionality when the value gets updated, as opposed to having different ways to access the value and mutate the value within the component, and scattered throughout the code.
If using the explicit way within a model, and not using setters, it seems like the methods
object is going to be cluttered up with updateValue
type methods for the template, as opposed to actual methods.
I guess it applies where the user selects an option in a component, and the component relies on that value to show what is selected.
You are relying on the parent component passing that back into the component for it to be able to do that.
Unless, when the user selects an option, you trigger the component display update manually. Which seems to be moving away from Vue's reactivity.
So having an internal value track what it 'should be', have the template react to that. Use setters/getters to wrap the property to track internal changes & raise external events, and a watch on the property to update the internal value when it is changed externally.
Perhaps I am just struggling to get my head around the new way of doing it.
@Towerful - You aren't the only one....
Scott
@Towerful:
To me it seems that what you describe are essentally components that act like inputs with a v-model
: the user changes some value in the UI, and you want that change to be reflected in the bound data immediatly.
For these types of components, you can use v-model
on comonents in 2.0:
<my-input v-model="myValue">
// in my-input.vue
<input :value="value" @change="$emit('input', $event.target.value)">
export default {
props: ['value'] //special prop received from v-model
}
This makes the interface to real <input>
elements and to custom input components essentially identical, and a two-way binding.
For more complex components that receive multiple props (and are not simple custom inputs, but more abstract), we discourage using .sync
because it becomes hard to reason about in most situations.
The parent should decide what to do with a value it receives from the child, it'S data should not be implicitly changed like .sync
does.
Can you provide an example that is not solvabel with the v-model approach above and still beneftis from using .sync
? That may be a better foundation for discussion than abstract theory.
Oh, how did I miss that?! Its definitely there in the OP, and there is even a discussion about this some comments ago! Now I feel a bit stupid.
Could the original post be updated to make it a little more clear that v-model
can be used on a component?
@LinusBorg Off the top of my head, I cannot think of a case where v-model
on a component would not work. I missed that part in the original post.
Even for complicated object components, it would just be a matter of nesting components. And this re-enforces single responsibility components.
It makes a lot more sense :)
@Towerful
Re: using v-model
. The thing is, v-model
is synchronous (in a way), while the cross-component data flow is inherently asynchronous because of the watcher queue (demo). I've seen this confuse a lot of people. One-way flow just makes it more explicit that props are not synchronous and forces you to not rely on them being synchronous (this is what you are trying to trick your way around).
Re: cluttered methods. For simple cases you can always do @value-updated="value = $arguments[0]"
. For complicated cases it's a good thing to have a method, where you can adjust the state to keep it consistent (e.g. trigger updates manually). Segway to the next point.
Re: moving away from reactivity. I don't agree with this statement. For simple cases you don't need magic to make the child pick up the value, updated by the value-updated="value = $arguments[0]"
.
For complicated cases, with .sync
props you would have to use watch
, but explicit watch
is not really a part of reactivity. It's an escape hatch, where you trigger your manual updates that can not be expressed as computed. And it's not a good one, because it can not react to changes synchronously, like computed can. This is why when using watch
heavily, you may find your data updates taking a few "ticks" to propagate. If you ever stumbled upon directly nested nextTick
, you know what I'm talking about.
Now, -updated
handler provides a better escape hatch, letting the complicated model changes be applied synchronously (or simultaneously asynchronously) after child expresses the intent, making sure that child will receive updated values in the next tick (or will not receive inconsistent state).
@yyx990803 Could we implement a way to listen for $emits, similar to how we had events for $dispatch and $broadcast in vuejs1?
Feels more vuejs-esque, something along the lines of this (an 'on' or 'listen'):
Vue.component('cart', {
template: "#cart-template",
data () {
return {quantity : 0 }
},
watch: {
'quantity': function (quantity, oldQuantity) {
console.log('quantity changed from %s to %s', oldQuantity, quantity)
bus.$emit('cart.quantity-changed', quantity)
}
}
});
new Vue({
el: '.container',
data : function () {
return {
quantity: 0
};
},
on: {
'cart.quantity-changed': function (newQuantity) {
console.log('quantity change emitted');
Vue.set(self, 'quantity', newQuantity);
}
},
computed:{
gold: function(){
return this.quantity * 100
}
}
})
This basically would automatically attach itself to the global bus.
Event Busses are not a pattern that we want to encourage - it is only useful some edge cases. Generally, a store pattern, like vuex, is preferred.
Implementing an API that makes using a bus easier and feels "officially supported" would be the wrong signal.
Looking at your example, if you stored the quantity in a store accessed by both components, no events would be nessessary. The computed property in the container
component would update automatically.
Simple example code without using a real store solution like vuex:
var store = {
cart: {
quantity: 0
}
}
Vue.component('cart', {
template: "#cart-template",
data () {
return store.cart
},
});
new Vue({
el: '.container',
data : function () {
return store.cart;
},
computed:{
gold: function(){
return this.quantity * 100
}
}
})
I would say the general idea of vue2 is to make it harder to shoot yourself in the foot.
On Sun, Jul 3, 2016 at 11:24 AM, Thorsten Lünborg notifications@github.com wrote:
Event Busses are not a pattern that we want to encourage - it is only useful some edge cases. Generally, a store pattern, like vuex, is preferred.
Implementing an API that makes using a bus easier and feels "officially supported" would be the wrong signal.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vuejs/vue/issues/2873#issuecomment-230158828, or mute the thread https://github.com/notifications/unsubscribe/AACoukCpCgYlDbVej_w_h4NEhQ-imYHBks5qR9QwgaJpZM4IedHC .
@kharysharpe $emit
s are intended to be listened with v-on
on the child instance. This also has an added benefit of being able to tap into the original context of where the instance is used:
<list-item v-for="(item, index) in items"
:title="item.title"
@remove="items.splice(index, 1)"
>
</list-item>
Is there a date for the 2.0 release? I'm pretty excited with the changes. Congrats! I'm thinking on using Vue 2.0 + Redux.
@Sendoushi No date yet on the final release, but beta may be within a week. 😄 Vuex 2.0 is also being developed alongside and it will not only feature a much simpler API than current vuex, but also integrates into Vue ecosystem much more nicely than redux.
Vuex 2.0 is also being developed alongside and it will not only feature a much simpler API than current vuex, but also integrates into Vue ecosystem much more nicely than redux.
@chrisvfritz That's fantastic to hear! I always felt the current API is a bit overly and unnecessarily complicated. Ended up having to do things like this to compensate:
const mutations = {
LOGIN (state) { state.loggedIn = true },
LOGOUT (state) { state.loggedIn = false }
}
export const types = Object.keys(mutations)
// For now we dynamically generate all the actions like this.
// It's rare when anything more complicated is needed, but there
// is an example here:
// http://vuex.vuejs.org/en/actions.html
export const actions = types.reduce((o, el) => {
var action = S(el.toLowerCase()).camelize().s
o[action] = ({dispatch}, ...args) => dispatch(el, ...args)
return o
}, {})
What about the roadmap of vue 2 and vuex 2. Is it planned to release them together or one before the other and what about the compatibilities of different versions?
Relating to question above, what is the status with vue-router -- is it going to get Vue 2 support soon or will Vue 2 testing need to be done without the router?
@gwildu They'll likely be released somewhat together and Vuex 2.0 will only support Vue 2.0. Pre 2.0 Vuex will still receive support until Vue 1.x is no longer supported.
@Uninen Vue Router will be receiving some love next, before the release of Vue 2.0.
Thanks for the nfo @chrisvfritz :)
@chrisvfritz @Uninen correction: Vuex 2.0 also works for Vue 1.x.
Next major version of vue-router
will only support Vue 2.x.
Runtime only build: since it doesn't include the compiler, you need to either pre-compiled templates in a compile step, or manually written render functions.
Is there / will there be a way to precompile templates without using vueify/vue-loader and .vue
files? If not, would it be a good idea to have a babel plugin to transform template:
properties to render
functions in components?
升级2.0后,deprecated的特性还能用不 可以先升级2.0,然后再慢慢把deprecated的特性改掉不
Will it be possible to create a terminal component, now that elementDirective is gone?
As mentioned:
v-model no longer cares about initial inline value. It will always treat the Vue instance data as the source of truth.
Consider that
<child-component>
<input type="checkbox" :id="_uid" v-model="childModel" :value="value" />
<label :for="_uid"><slot></slot></label>
</child-component>
How can I deal with checkbox with arrays between custom components?
Updated 1:
Solved. Transfer prop type="checkbox"
to the child component in the parent component.
<parent-component>
<child-component type="checkbox" v-model="parentModel" value="apple" />
<child-component type="checkbox" v-model="parentModel" value="orange" />
<child-component type="checkbox" v-model="parentModel" value="banana" />
</parent-component>
Then you can get inline value via {props: [ 'value' ]}
.
Emit a change
event to the parent component to tell the value has changed.
<child-component>
<input type="checkbox" :id="_uid" v-model="childModel" :value="value"
@change="$emit('change', $event)"
/>
<label :for="_uid"><slot></slot></label>
</child-component>
This is because the compiler compiles the v-model
directive according to its type
. And the compiler will generate the checked
prop and bind a change
event to it.
Updated 2:
However, the updated
life cycle hook doesn't trigger due to v-model
directly changes the checked
attribute (That means you can't get a change
event of a native html checkbox component by modifying the value of v-model
).
So, @yyx990803, can you trigger a change
event after v-model
changes?
@YunSun-CN The only way that I was able to get around your issue was to add a property specific for the value, ala val, and use that to set the actual value, and then just emit changes to the v-model 'input' event.
@johnleider I wrote a custom directive to simulate what v-model
does.
By the way, you should generate models in a strict way not only checking type
prop but checking element's tagName. Otherwise, another custom component with type
prop may overwrite its default model behaviour.
Both of these are being deprecated in favor of more explicit named indices and keys. This syntax is a bit magical and has limitations in nested loops. As a bonus, there will be two fewer points of syntax for newcomers to learn.
value in arr
(value, index) in arr
(switched order of arguments to be more consistent with JavaScript'sforEach
andmap
)value in obj
(value, key) in obj
(switched order of arguments, partly to be more consistent with many common object iterators, such as lodash's)(value, key, index) in obj
(index will now be available in object iteration for visual purposes, such as table striping)Directive interface change
In general, in 2.0 directives have a greatly reduced scope of responsibility: they are now only used for applying low-level direct DOM manipulations. In most cases, you should prefer using Components as the main code-reuse abstraction.
Directives no longer have instances - this means there's no more
this
inside directive hooks andbind
,update
andunbind
now receives everything as arguments.Note the
binding
object is immutable, settingbinding.value
will have no effect, and properties added to it will not be persisted. You can persist directive state onel
if you absolutely need to:You can use destructuring if you only care about the value:
In addition, the
update
hook has a few changes:bind
.binding.value === binding.oldValue
to skip unnecessary updates, but there are also cases where you'd want to always apply updates, e.g. when the directive is bound to an Object that might have been mutated instead of replaced.elementDirective
, directive params and directive options such asacceptStatement
,deep
etc. are all deprecated.Filter Usage and Syntax Change
In Vue 2.0, there are several changes to the filter system:
{{}}
tags). In the past we've found using filters with directives such asv-model
,v-on
etc. led to more complexity than convenience, and for list filtering onv-for
it is more appropriate to move that logic into JavaScript as computed properties.The filter syntax has changed to be more inline with JavaScript function invocation, instead of taking space-delimited arguments:
Transition System
Transition CSS class changes:
The always-on
v-transition
class is no longer added and Vue now uses the same classes Angular and React CSSTransitionGroup does:v-enter
: applied before element is inserted, remove after 1 tick. (starting state for enter)v-enter-active
: applied before element is inserted, removed when transition/animation finishes. (active + ending state for enter)v-leave
: applied right when the leave transition is triggered, remove after 1 tick (starting state for leave)v-leave-active
: applied right when the leave transition is triggered, removed when the transition/animation finishes. (active + ending state for leave)v-enter-active
andv-leave-active
gives you the ability to specify different easing curves for enter/leave transitions. In most cases, upgrading means simply replacing your currentv-leave
withv-leave-active
. (For CSS animations, usev-enter-active
+v-leave-active
)Transition API Change
The
<transition>
componentAll single-element transition effects are now applied by wrapping the target element/component with the
<transition>
built-in component. This is an abstract component, which means it does not render an extra DOM element, nor does it show up in the inspected component hierarchy. It simply applies the transition behavior to the wrapped content inside.The simplest usage example:
The component defines a number of props and events that maps directly to the old transition definition options:
Props
Used to automatically generate transition CSS class names. e.g.
name: 'fade'
will auto expand to.fade-enter
,.fade-enter-active
, etc. Defaults to"v"
.Whether to apply transition on initial render. Defaults to
false
.Whether to apply CSS transition classes. Defaults to
true
. If set tofalse
, will only trigger JavaScript hooks registered via component events.Specify the type of transition events to wait for to determine transition end timing. Available values are
"transition"
and"animation"
. By default, it will automatically detect the type that has a longer duration.Controls the timing sequence of leaving/entering transitions. Available modes are
"out-in"
and"in-out"
; defaults to simultaneous.Individually configure transition CSS classes.
Example applying transition to dynamic components:
Events
Corresponds to the JavaScript hooks available in 1.x API.
Example:
When the entering transition completes, the component's
transitionComplete
method will be called with the transitioned DOM element as the argument.Some notes:
leave-cancelled
is no longer available for insertion/removals. Once a leave transition starts, it cannot be cancelled. It is, however, still available forv-show
transitions.enter
andleave
hooks, the presence ofcb
as the second argument indicates the user wants explicit control of the ending timing of the transition.The
<transition-group>
componentAll multi-element transition effects are now applied by wrapping the elements with the
<transition-group>
built-in component. It exposes the same props and events as<transition>
does. The difference being that:<transition>
,<transition-group>
renders a real DOM element. By default it renders a<span>
, and you can configure what element is should render via thetag
prop. You can also use it with theis
attribute, e.g.<ul is="transition-group">
.<transition-group>
does not support themode
prop.<transition-group>
must be uniquely keyed.Example:
Moving Transitions
<transition-group>
supports moving transitions via CSS transform. When a child's position on screen has changed after an updated, it will get applied a moving CSS class (auto generated from thename
prop or configured with themoveClass
prop). If the CSStransform
property is "transition-able" when the moving class is applied, the element will be smoothly animated to its destination using the FLIP technique.See a live demo here.
Creating Reusable Transitions
Now that transitions are applied via components, they are no longer considered an asset type, so the global
Vue.transition()
method and thetransition
option are both deprecated. You can just configure the transition inline with component props and events. But how do we create reusable transition effects now, especially those with custom JavaScript hooks? Well, the answer is creating your own transition components (they are particularly suitable as functional components):You can then use it like this:
v-model changes
The
lazy
andnumber
params are now modifiers:.trim
- trims the input, as the name suggests.debounce
param has been deprecated. (See upgrade tip at bottom)v-model
no longer cares about initial inlinevalue
. It will always treat the Vue instance data as the source of truth. This means the following will render with a value of 1 instead of 2:Same goes for
<textarea>
with existing content. So instead of:Do:
The main idea is that the JS side should be considered the source of truth, not your templates.
v-model
no longer works when used on av-for
iterated primitive value:This doesn't work because it's the equivalent of this in JavaScript:
As you can see, setting
str
to another value in the iterator function will do nothing because it's just a local variable in the function scope. Instead, you should use an array of objects so thatv-model
can update the field on the object:Props Behavior
.once
and.sync
are deprecated. Props are now always one-way down. To produce side effects in the parent scope, a component needs to explicitly emit an event instead of relying on implicit binding.a
and then setthis.a = someOtherValue
in the component. Due to the new rendering mechanism, whenever the parent component re-renders, the child component's local changes will be overwritten. In general, in 2.0 you should treat props as immutable. Most use cases of mutating a prop can be replaced by either a data property or a computed property.keep-alive
keep-alive
is no longer a special attribute: it is now a wrapper component, similar to<transition>
:This makes it possible to use
keep-alive
on multiple conditional children (note the children should eventually evaluate to a single child - any child other than the first one will be ignored):When used together with
<transition>
, make sure to nest it inside:Slots
<slot>
s with the same name in the same template. When a slot is rendered it is "used up" and cannot be rendered elsewhere in the same render tree.<slot>
no longer preserves theslot
attribute. Use a wrapper element to style them, or, for advanced use cases, modify the inserted content programmatically using render functions.Refs
v-ref
is now no longer a directive: it is now a special attribute similar tokey
andtransition
:Dynamic ref bindings are now also supported:
vm.$els
andvm.$refs
are merged. When used on a normal element the ref will be the DOM element, and when used on a component the ref will be the component instance.vm.$refs
are no longer reactive, because they are registered/updated during the render process itself. Making them reactive would require duplicate renders for every change.On the other hand,
$refs
are designed primarily for programmatic access in JavaScript - it is not recommended to rely on$refs
in templates because it entails referring to state that does not belong to the instance itself.Misc
track-by
has been replaced withkey
. It now follows the same rule for binding an attribute: withoutv-bind:
or:
prefix, it is treated as a literal string. In most cases you'd want to use a dynamic binding, which expects a full expression instead of a string key. For example:Interpolation inside attributes are deprecated:
Attribute binding behavior change: only
null
,undefined
andfalse
are considered falsy when binding attributes. This means0
and empty strings will render as-is. For enumerated attributes. This means:draggable="''"
will render asdraggable="true"
.Also, for enumerated attributes, in addition to the falsy values above, the string value of "false" will also render as attr="false".
v-on
now only listens to custom events $emitted by that component. (no longer listens to DOM events)v-else
no longer works withv-show
- just use negation expression.{{* foo }}
) deprecated - usev-once
instead.:style
no longer supports inline!important
propsData
instead)el
option can no longer be used inVue.extend
. It can now only be used as an instance creation option.Vue.set
andVue.delete
cannot work on Vue instances. It is now mandatory to properly declare all top-level reactive properties in thedata
option.$data
. This prevents some edge cases in the reactivity system and makes the component state more predictable (especially with type-checking systems).User watchers created via
vm.$watch
are now fired before the associated component re-renders. This gives the user a chance to further update other state before the component re-render, thus avoiding unnecessary updates. For example, you can watch a component prop and update the component's own data when the prop changes.To do something with the DOM after component updates, just use the updated lifecycle hook.
Upgrade Tips
How to Deal with Deprecation of
$dispatch
and$broadcast
?The reason that we are deprecating
$dispatch
and$broadcast
is that event flows that depend on the components tree structure can be hard to reason about when the components tree becomes large (simply put: it doesn't scale well in large apps and we don't want to set you up for pain later).$dispatch
and$broadcast
also do not solve the communication between sibling components. Instead, you can use a pattern similar to the EventEmitter in Node.js: a centralized event hub that allows components to communicate, no matter where they are in the components tree. Because Vue instances implement the event emitter interface, you can actually use an empty Vue instance for that purpose:And don't forget to use $off to unbind the event.
This pattern can serve as a replacement for
$dispatch
and$broadcast
in simple scenarios. But for more complex cases, it is recommended to introduce a dedicated state management layer using Vuex.How to Deal with the Deprecation of Array Filters?
For list filtering with
v-for
- one of the more common usage of filters - it is now recommended to use computed properties that return a processed copy of the original Array (see updated data grid example). The benefits is that you are no longer limited by the arbitrary filter syntax/API - it's just plain JavaScript now, and you naturally have access to the filtered result because it is a computed property.Also see this discussion thread.
How to Deal with the Deprecation of
debounce
forv-model
?Debouncing is used to limit how often we execute Ajax requests and other expensive operations. Vue's
debounce
attribute parameter forv-model
makes this easy, but it also debounces state updates rather than the expensive operations themselves, which comes with limitations.These limitations become apparent when designing a search indicator. Take a look at that example. Using the
debounce
attribute, there'd be no way to detect a dirty input before the search begins, because we'd lose access to the input's real-time state. By decoupling the debounce function from Vue, we're able to debounce only the operation we want to limit.There will be other times when debouncing isn't quite the right wrapper function. In the very common example of hitting an API for search suggestions, waiting to offer suggestions until after the user has stopped typing isn't an ideal experience. What you probably want instead is a throttling function. Now since you're already using a utility library like lodash for
debounce
, refactoring to usethrottle
instead takes only a few seconds!