vue使用动态组件实现TAB切换效果完整实例

一、方法1:使用Vant组件库的tab组件

Vant 2 – Mobile UI Components built on vue

-1

二、 方法2:手动创建tab切换效果

-2

-3

1.在components文件夹下创建切换的.vue页面、引入使用

  1. import one from “./components/one”;
  2. import two from “./components/two”;
  3. import three from “./components/three”;
  4. import four from ./components/four”;
  5. components: {
  6.      one,
  7.      two,
  8.      three,
  9.      four,
  10. },

2.布局:上面放tab点击的标签,下面放组件呈现对应内容

  1. // 然后使用v-for循环出来呈现
  2. <template>
  3.      <div id=“app”>
  4.          <div class=“top”>
  5.          <!– 放置tab点击标签 –>
  6.              <div class=“crad”
  7.              :class=“{ highLight: whichIndex == index }”
  8.              vfor=“(item, index) in cardArr”
  9.              :key=“index”
  10.              @click=“whichIndex = index”>
  11.              {{ item.componentName }}
  12.          </div>
  13.          </div>
  14.          <div class=“bottom”>
  15.          <!– 放置动态组件… –>
  16.          <!– keepalive缓存组件,这样的话,组件就不会被销毁,DOM就不会被重新渲染,
  17.          浏览器也就不会回流和重绘,就可以优化性能。不使用的话页面加载就会慢一点 –>
  18.          <keepalive>
  19.          <component :is=“componentId”></component>
  20.          </keepalive>
  21.          </div>
  22.      </div>
  23. </template>

3.写好上面的tab点击标签,定义数据修改样式

  1. // 首先我们在data中定义数组cardArr存放点击tab的数据
  2. data() {
  3.      return {
  4.          whichIndex: 0,
  5.          cardArr: [
  6.          {
  7.              componentName: “动态组件一”,
  8.              componentId: “one”,
  9.          },{
  10.              componentName: “动态组件二”,
  11.              componentId: “two”,
  12.          },{
  13.              componentName: “动态组件三”,
  14.              componentId: “three”,
  15.          },{
  16.              componentName: “动态组件四”,
  17.              componentId: “four”,
  18.          },
  19.          ],
  20.      };
  21. },
  1. // 又因为需要有高亮状态样式:默认索引0高亮
  2. .highLight {
  3.      boxshadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  4.      transform: translate3d(0, 1px, 0);
  5. }

三、完整代码

  1. <template>
  2.      <div id=“app”>
  3.      <div class=“top”>
  4.          <div
  5.          class=“crad”
  6.          :class=“{ highLight: whichIndex == index }”
  7.          v-for=“(item, index) in cardArr”
  8.          :key=“index”
  9.          @click=
  10.              whichIndex = index;
  11.              componentId = item.componentId;
  12.          “
  13.          >
  14.          {{ item.componentName }}
  15.          </div>
  16.      </div>
  17.      <div class=“bottom”>
  18.          <keep-alive>
  19.          <component :is=“componentId”></component>
  20.          </keep-alive>
  21.      </div>
  22.      </div>
  23. </template>
  24. <script>
  25. import one from “./components/one”;
  26. import two from “./components/two”;
  27. import three from “./components/three”;
  28. import four from “./components/four”;
  29. export default {
  30.      components: {
  31.      one,
  32.      two,
  33.      three,
  34.      four,
  35.      },
  36.      data() {
  37.      return {
  38.          whichIndex: 0,
  39.          componentId: “one”,
  40.          cardArr: [
  41.          {
  42.              componentName: “动态组件一”,
  43.              componentId: “one”,
  44.          },
  45.          {
  46.              componentName: “动态组件二”,
  47.              componentId: “two”,
  48.          },
  49.          {
  50.              componentName: “动态组件三”,
  51.              componentId: “three”,
  52.          },
  53.          {
  54.              componentName: “动态组件四”,
  55.              componentId: “four”,
  56.          },
  57.          ],
  58.      };
  59.      },
  60. };
  61. </script>
  62. <style lang=“less” scoped>
  63. #app {
  64.      width: 100%;
  65.      height: 100vh;
  66.      boxsizing: borderbox;
  67.      padding: 50px;
  68.      .top {
  69.      width: 100%;
  70.      height: 80px;
  71.      display: Flex;
  72.      justifycontent: spacearound;
  73.      .crad {
  74.          width: 20%;
  75.          height: 80px;
  76.          lineheight: 80px;
  77.          textalign: center;
  78.          backgroundcolor: #fff;
  79.          border: 1px solid #e9e9e9;
  80.      }
  81.      .highLight {
  82.          boxshadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  83.          transform: translate3d(0, 1px, 0);
  84.      }
  85.      }
  86.      .bottom {
  87.      margintop: 20px;
  88.      width: 100%;
  89.      height: calc(100%  100px);
  90.      border: 3px solid pink;
  91.      display: flex;
  92.      justifycontent: center;
  93.      alignitems: center;
  94.      }
  95. }
  96. </style>

总结

到此这篇关于vue使用动态组件实现TAB切换效果的文章就介绍到这了,更多相关vue动态组件实现TAB切换内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论