码迷,mamicode.com
首页 > 其他好文 > 详细

vue2.0 动态切换组件

时间:2017-09-08 16:19:06      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:com   hang   button   script   who   his   charset   class   vue2   

组件标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../vue2.2.js"></script>
    <title>component-4</title>
</head>
<body>
    <h1>component-4</h1>
    <hr>
    <div id="app">
       <component v-bind:is="who"></component>
       <button @click="changeComponent">changeComponent</button>
    </div>
 
    <script type="text/javascript">
        var componentA={
            template:`<div style="color:red;">this componentA</div>`
        }
        var componentB={
            template:`<div style="color:green;">thiscomponentB</div>`
        }
        var componentC={
            template:`<div style="color:pink;">this componentC</div>`
        }
       
        var vm=new Vue({
            el:#app,
            data:{
                who:componentA
            },
            components:{
                "componentA":componentA,
                "componentB":componentB,
                "componentC":componentC,
            },
            methods:{
                changeComponent:function(){
                    if(this.who==componentA){
                        this.who=componentB;
                    }else if(this.who==componentB){
                        this.who=componentC;
                    }else{
                        this.who=componentA;
                    }
                }
            }
        })
    </script>
</body>
</html>

 

vue2.0 动态切换组件

标签:com   hang   button   script   who   his   charset   class   vue2   

原文地址:http://www.cnblogs.com/lhl66/p/7494927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!