1. 程式人生 > >vue2.0 動態切換組件

vue2.0 動態切換組件

scrip 2.0 動態 com 不同的 cti meta change temp

組件標簽是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 動態切換組件