其他分享
首页 > 其他分享> > 动态组件 is vue

动态组件 is vue

作者:互联网

<!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>
    <style>
        .tabbtn{
            background-color: greenyellow;
        }
        /* 交集选择器 */
        .tabbtn.active{
            background-color: hotpink;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <button v-for="(item,index) in btns" :class=['tabbtn',{'active':tab==item+'-com'}] :key="index" @click="btnhandler(item)">{{item}}</button>
            <p :is="tab">qweqwe</p>
        </div>
    </div>
    <script src="../../vue.js"></script>
    <script>

        Vue.component("tab1-com", {template: "<h1>tab1</h1>"});
        Vue.component("tab2-com", {template: "<h1>tab2</h1>"});
        Vue.component("tab3-com", {template: "<h1>tab3</h1>"});
        Vue.component("tab4-com", {template: "<h1>tab4</h1>"});
        // root组件
        const vm = new Vue({
            el: '#app',
            data: {
                tab:'tab1-com',
                btns: ['tab1', 'tab2', 'tab3', 'tab4']
            },
            methods: {
                btnhandler(e) {
                    this.tab = e + '-com';
                }
            },
        });

    </script>
</body>

</html>

标签:vue,tab,tab1,component,Vue,template,组件,动态,com
来源: https://blog.csdn.net/qq_44917015/article/details/116208754