前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue3动态组件

Vue3动态组件

原创
作者头像
软件工程师Michael
发布2022-10-06 19:31:41
1.1K0
发布2022-10-06 19:31:41
举报

先上Vue3组件的实例代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

</head>
<body>
    <div id="michael">
        <michael></michael>
        <sky></sky>
        <blue></blue>
    </div>
    <script>
       var app= Vue.createApp({
            data(){
                return {
                    "msg":"hello"
                }
            }

        });
        app.component('michael', {
            'template':`
                <div>michael</div>
            `
        });
        app.component('sky', {
            'template':`
                <div>sky</div>
            `
        });
        app.component('blue', {
            'template':`
                <div>blue</div>
            `
        });
        app.mount("#michael");
    </script>
</body>
</html>

注意:以上是Vue3的叠加的写法,不一定要写成链式的代码。

运行效果:

多个组件
多个组件

动态组件的写法:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        .abc{
            width: 100px;
            height: 100px;
            border: 1px solid black;
            background-color: green;
            color:white;
            
        }
    </style>

</head>
<body>
    <div id="michael">
        <!--<michael></michael>
        <sky></sky>
        <blue></blue>-->
        <button v-for='tab in btnMsg' :key="tab" @click="tabName=tab">
            {{tab}}
        </button>
        <component :is="tabName" class="abc"></component><!--动态组件-->
    </div>
    <script>
       var app= Vue.createApp({
            data(){
                return {
                    "msg":"hello",
                    "btnMsg":["michael","sky","blue"],
                    "tabName":"michael"
                }
            }

        });
        app.component('michael', {
            'template':`
                <div>michael</div>
            `
        });
        app.component('sky', {
            'template':`
                <div>sky</div>
            `
        });
        app.component('blue', {
            'template':`
                <div>blue</div>
            `
        });
        app.mount("#michael");
    </script>
</body>
</html>

运行效果如下:

动态组件的效果
动态组件的效果

[小结]

  • 在Vue3中,可以使用component标签进行组件输出
  • component标签需要配合:is属性来指定输出的组件名称,属性值为字符串
  • component标签的所有的属性都会叠加到最终输出组件内容的最外层元素上

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com