karol-f / vue-custom-element

Vue Custom Element - Web Components' Custom Elements for Vue.js
https://karol-f.github.io/vue-custom-element/
MIT License
1.97k stars 187 forks source link

feat: return root instance if not mounted #182

Closed pimlie closed 4 years ago

pimlie commented 4 years ago

This pr supports delayed mounting of the Vue app by returning the root Vue instance in getVueInstance if no children exists yet

This makes the following possible:

let customElementRoot
...
  beforeCreateVueInstance(root) {
    customElementRoot = root.el
    delete root.el
    return root
  },
  vueInstanceCreatedCallback() {
    const app = this.getVueInstance()
    // do some stuff with the vue instance
    app.$mount(customElementRoot)
  }
karol-f commented 4 years ago

Can You please elaborate what is the use case of this PR. What problem it solves?

Avoiding errors when there's delayed mounting?

Is there use case that can be maybe solved other way?

pimlie commented 4 years ago

I've created a module/wrapper to use a Nuxt SPA as a custom element. Although it seems to work fine already, Nuxt does some extra initialization between creating the Vue instance and mounting the instance. To prevent any differences in behaviour I'd like to stay as close as possible to the default Nuxt implementation, hence this pr.

karol-f commented 4 years ago

Merged and released in https://github.com/karol-f/vue-custom-element/releases/tag/v3.2.10

pimlie commented 4 years ago

Damn you are fast, it must be a new Github record from pr to review to release! Many thanks ;)

pimlie commented 4 years ago

Hmm, could you please verify if the npm publish was succesfull? It could also be that the npm cache is bogging again, but the 3.2.10 release is not yet listed: https://www.npmjs.com/package/vue-custom-element

karol-f commented 4 years ago

Checking...

karol-f commented 4 years ago

Done, sorry for the inconvenience.