Home  >  Q&A  >  body text

Create an instance of a vue 3 component using the composition api

<p>Suppose I create a component in vue3 using the following syntax</p> <pre class="brush:php;toolbar:false;">// Message.vue <script setup lang="ts"> const props = defineProps<{ message: string }>(); </script> <template> <span>{{ props.message }}</span> </template></pre> <p>How do I create an instance of this message component? </p> <pre class="brush:php;toolbar:false;">import Message from "./Message" let message1 = Message({message:"hello world"}); // how to do this?</pre> <p>This way I can use <em>message1</em> with <code><component></code> like <code><component :is="message1" / ></code></p>
P粉436688931P粉436688931419 days ago416

reply all(1)I'll reply

  • P粉510127741

    P粉5101277412023-08-28 09:16:38

    You can add the following imports to the file.

    import { createApp, h } from "vue";

    Then create the message component this way.

    let message1 = createApp({ 
      setup () {
        return () => h(Message, {message: "hello world"});
      }
    });

    reply
    0
  • Cancelreply