Create an instance of a vue 3 component using the composition api
P粉436688931
P粉436688931 2023-08-27 10:23:06
0
1
413
<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粉436688931
P粉436688931

reply all(1)
P粉510127741

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"});
  }
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!