使用composition api创建vue 3组件的实例
P粉436688931
P粉436688931 2023-08-27 10:23:06
[Vue.js讨论组]
<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>
P粉436688931
P粉436688931

全部回复(1)
P粉510127741

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

import { createApp, h } from "vue";

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

let message1 = createApp({ 
  setup () {
    return () => h(Message, {message: "hello world"});
  }
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号