0

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 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

No, this is not a case. The slots are hardcoded, and Im looking for solution without additional global components, only with instance.
@Molfar sorry, but I can't understand what are you looking. Slots are HTML arguments from parent component to child.
I need to understand how innerhtml inside any <component-name>some text or tags</component-name> is parsed into slots, named and scoped. Where it happens?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.