AI编程助手
AI免费问答

使用composition api创建vue 3组件的实例

<p>假设我在 vue3 中使用以下语法创建了一个组件</p> <pre class="brush:php;toolbar:false;">// Message.vue &lt;script setup lang=&quot;ts&quot;&gt; const props = defineProps&lt;{ message: string }&gt;(); &lt;/script&gt; &lt;template&gt; &lt;span&gt;{{ props.message }}&lt;/span&gt; &lt;/template&gt;</pre> <p>如何创建此消息组件的实例?</p> <pre class="brush:php;toolbar:false;">import Message from &quot;./Message&quot; let message1 = Message({message:&quot;hello world&quot;}); // how to do this?</pre> <p>这样我就可以将 <em>message1</em> 与 <code><component></code> 一起使用,例如 <code><component :is="message1" /></code></p>
# Vue.js
P粉436688931 P粉436688931 675 天前 877 次浏览

全部回复(1) 我要回复

  • P粉510127741

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

    您可以将以下导入添加到文件中。

    import { createApp, h } from "vue";

    然后以这种方式创建消息组件。

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

    回复
    0
  • 取消 回复 发送