vuejs2.0 组件基础动态组件 Tab选项卡插件

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Document</title>

    <script src="https://unpkg.com/vue"></script>

    <style>

      .tab-button {

        padding: 6px 10px;

        border-top-left-radius: 3px;

        border-top-right-radius: 3px;

        border: 1px solid #ccc;

        cursor: pointer;

        background: #f0f0f0;

        margin-bottom: -1px;

        margin-right: -1px;

      }

      .tab-button:hover {

        background: #e0e0e0;

      }

      .tab-button.active {

        background: #e0e0e0;

      }

      .tab {

        border: 1px solid #ccc;

        padding: 10px;

      }

    </style>

  </head>

  <body>

    <div id="dynamic-component-demo" class="demo">

      <button

        v-for="tab in tabs"

        v-bind:key="tab"

        v-bind:class="['tab-button', { active: currentTab === tab }]"

        v-on:click="currentTab = tab"

      >

        {{ tab }}

      </button>

      <component v-bind:is="currentTabComponent" class="tab"></component>

    </div>

    <script>

      Vue.component("tab-home", {

        template: "<div>Home component</div>"

      });

      Vue.component("tab-posts", {

        template: "<div>Posts component</div>"

      });

      Vue.component("tab-archive", {

        template: "<div>Archive component</div>"

      });

      new Vue({

        el: "#dynamic-component-demo",

        data: {

          currentTab: "Home",

          tabs: ["Home", "Posts", "Archive"]

        },

        computed: {

          currentTabComponent: function() {

            return "tab-" + this.currentTab.toLowerCase();

          }

        }

      });

    </script>

  </body>

</html>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 下载安装搭建环境 可以选npm安装,或者简单下载一个开发版的vue.js文件 浏览器打开加载有vue的文档时,控制...
    冥冥2017阅读 11,276评论 0 42
  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 6,693评论 0 6
  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 4,892评论 0 1
  • 第一节 vue:读音: v-u-eview vue和angular区别?vue——简单、易学指令以 v-xxx一片...
    黑色的五叶草阅读 4,799评论 0 1
  • 基本格式 以json的形式、将数据(支持所有格式)挂载在vue的data上、方法挂载在vue的methods上。 ...
    kirito_song阅读 4,266评论 0 21

友情链接更多精彩内容