Vue: [Suggestion] Vue 2.0 - vm.$appendTo - singleton components

Created on 29 Apr 2016  路  3Comments  路  Source: vuejs/vue

My current singleton pattern is to Vue.extend the component and call $appendTo(body) on compile on the first require and return the vm on all others.

When vm.$appendTo will be removed, I will need another way of creating singleton components..
Maybe something like this:

replace: false
el: => document.body

In vue-comps I have currently 3 singleton components:

  • vue-comps-waves - has a hidden singleton svg store which serves all waves instances with the svg animations
  • vue-toster - the same instance should be shared by all components which use it
  • vue-overlay - a single overlay to black out the background which keeps track of the z-index of all components which use it
discussion

Most helpful comment

You need to call $mount() without arguments (which creates vm.$el) before appending it, but otherwise yes it should work.

All 3 comments

I don't quite understand the need for $appendTo() here - you can just use native DOM methods on this.$el, what's the difference?

Maybe I'm just confused about

attached deprecated, components no longer have possibility to be off-dom

Will this work?

Comp = Vue.extend(someComp)
vm = new Comp() # vm is off-dom here ?
document.body.appendChild(vm.$el)

if so, this can be closed..

You need to call $mount() without arguments (which creates vm.$el) before appending it, but otherwise yes it should work.

Was this page helpful?
0 / 5 - 0 ratings