vue.js标签页切换

运行后效果如下:


[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="UTF-8">  
  5.   <title>Vue.js小demo</title>  
  6.   <style>  
  7.   h3{  
  8.     margin-top: 10px;  
  9.   }  
  10.   #content{  
  11.     width: 400px;  
  12.     margin: 30px auto;  
  13.     background: #eee;  
  14.     padding:10px;  
  15.     overflow: hidden;  
  16.   }  
  17.   .nav{  
  18.     width: 100%;  
  19.     height: 24px;  
  20.     list-style: none;  
  21.   }  
  22.   .nav li a{  
  23.     width: 30%;  
  24.     float: left;  
  25.   }  
  26.   .tab-con{  
  27.     min-height: 200px;  
  28.     margin-top: 10px;  
  29.     width: 90%;  
  30.     border-radius: 4px;  
  31.     background: #cfcfcf;  
  32.     margin:0 auto;  
  33.     padding:10px;  
  34.   }  
  35.   </style>  
  36. </head>  
  37. <body>   
  38. <template id="temp-tab01">  
  39.     <div>this is tab01</div>  
  40. </template>  
  41. <template id="temp-tab02">  
  42.     <div>this is tab02</div>  
  43. </template>  
  44. <template id="temp-tab03">  
  45.     <div>this is tab03</div>  
  46. </template>  
  47. <div id="content">  
  48.     <h3>动态组件</h3>  
  49.     <!-- 导航栏 -->  
  50.     <ul class="nav">  
  51.         <li><a href="javascript:void(0);" @click="toggleTabs(tab01Text)">{{tab01Text}}</a></li>  
  52.         <li><a href="javascript:void(0);" @click="toggleTabs(tab02Text)">{{tab02Text}}</a></li>  
  53.         <li><a href="javascript:void(0);" @click="toggleTabs(tab03Text)">{{tab03Text}}</a></li>  
  54.     </ul>  
  55.      <!-- 点击导航后要切换的内容 -->  
  56.      <div class="tab-con">  
  57.          <component :is="currentView" keep-alive></component>  
  58.      </div>  
  59. </div>  
  60. </body>  
  61. <script src="https://cdn.bootcss.com/vue/2.2.6/vue.js"></script>  
  62. <script>  
  63. //扩展组件tab01  
  64. var tab01=Vue.extend({  
  65.     template:"#temp-tab01"  
  66. });  
  67. var tab02=Vue.extend({  
  68.     template:"#temp-tab02"  
  69. });  
  70. var tab03=Vue.extend({  
  71.     template:"#temp-tab03"  
  72. });  
  73.   
  74. var dr01=new Vue({  
  75.     el:"#content",  
  76.     data:{  
  77.         tab01Text:"tab01",  
  78.         tab02Text:"tab02",  
  79.         tab03Text:"tab03",  
  80.         currentView:'tab01'//默认选中的导航栏  
  81.     },  
  82.     //局部注册组件  
  83.     components:{  
  84.         tab01:tab01,  
  85.         tab02:tab02,  
  86.         tab03:tab03  
  87.     },  
  88.     methods:{  
  89.         //绑定tab的切换事件  
  90.         toggleTabs:function(tabText){  
  91.             this.currentView=tabText;  
  92.         }  
  93.     }  
  94. });  
  95. </script>  
  96. </html>  

猜你喜欢

转载自blog.csdn.net/baozhiqiangjava/article/details/80809599