vue动态组件

vue动态组件

<html>
    <head>
        <title>动态组件</title>
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
        </script>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <div>
                <span @click="index=0">home</span>
                <span @click="index=1">index</span>
            </div>
            <component :is="currentTabComponent"> </component>

        </div>
    </body>
    <script>
        let home={
            template:`<p>home</p>`
        }
        let index={
            template:`<p>index</p>`
        }

        // vue实例
        let vue =new Vue({
            el:'#app',  
            data:{
                index:0,
                componentList:[
                    home,
                    index
                ]
            },
            computed:{
                currentTabComponent:function(){
                    return this.componentList[this.index]
                }
            },   
        });
    </script>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。