0
votes

How can I initialize new Vue instance, mount it to existing dom element, and use this element's html as component's (or app's) content?

Here is an example project https://codesandbox.io/s/white-darkness-f3v80

<div id="app">
   <p>This is slot text from index.html</p>
   <template slot="foo">Also should be parsed as named slot</template>
</div>

Element div#app (index.html) may contain default slot, or other kind of slots (scoped etc). And App.vue should parse this html into relevant slots.

1

1 Answers

1
votes

Try something like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <div>
        <any-component>
          <p>This is slot text from index.html</p>
          <template v-slot:foo>
            Also should be parsed as named slot
          </template>
        </any-component>
      </div>
    </div>
    <script>
      Vue.component('any-component', {
        template: `
          <div>
            <slot name="default"></slot>
            <slot name="foo"></slot>
          </div>`,
      });

      new Vue({
        el: '#app',
        data() {
          return {};
        },
      });
    </script>
  </body>
</html>

Are you looking for it?