vuejs / Discussion

Vue.js discussion
166 stars 17 forks source link

Vue 2 standalone Vue.compile rendering? #1266

Closed randohinn closed 7 years ago

randohinn commented 7 years ago

So, with the introduction of Vue2, the compile function has changed a bit, thus I am having difficulties rendering things via compile

Here's my code:

Vue.compile(this.$el.outerHTML);

This is in a VueComponent, where I include some additional HTML templates from separate files via vue-resource, which I then need to render. In Vue 1.x, calling compile also caused the template to re-render, however this does not happen in Vue 2. How can I re-render with the compilefunction now?

LinusBorg commented 7 years ago

In 2.0, the compile() function does not change the DOM. it only converts the template string into render functions.

Generally, what you should rather do, is something like this:

var tempComponent = new Vue({
  // template string here
  template: `<div><!-- whatever --></div>`
  // makes $parent work
  parent: this 
  // any props the component should receive. 
  // reference to data in the template will *not* have access the the current 
  // components scope, as you create a new component
  propsData:
  // possibly other options
}).$mount(/*element to mount to here*/)

(Sidenote: Specifically, it confuses me that you seem to want to use the current component's rendered HTML (this.$el.outerHTML) as a template? What should that achieve? the rendered HTML of a component does not contain any template markup for Vue)

randohinn commented 7 years ago

@LinusBorg Thanks, that does sound more logical, thanks... that was easy! 😄