最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實現(xiàn)多個tab標簽頁的切換與關閉詳細代碼

 更新時間:2023年10月12日 11:58:04   作者:嫣嫣細語  
這篇文章主要給大家介紹了關于vue實現(xiàn)多個tab標簽頁的切換與關閉的相關資料,使用vue.js實現(xiàn)tab切換很簡單,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下

1.實現(xiàn)效果

2.實現(xiàn)原理 

vuex,實現(xiàn)對當前激活項,當前tab列表,當前tab的translateX,當前緩存頁,當前路由的狀態(tài)管理。

將vuex中的數(shù)據(jù)保存到sessionStorage中,避免頁面刷新丟失,當瀏覽器關閉時,清空數(shù)據(jù)。

通過ref定位,拿到當前窗口寬度與當前所在路由的tab標簽的所有寬度,判斷兩者,實現(xiàn)對多tab超出窗口寬度的處理。

當點擊tab標簽頁的時候,獲取相應的激活項,動態(tài)的實現(xiàn)左側菜單欄的選中狀態(tài),用watch監(jiān)聽,updateActiveName和updateOpened。

當關閉tab標簽的時候,splice刪除當前標簽,若是刪除的最后一項,跳轉到該項的前一項頁面。當長度為1時,跳轉到首頁。

3.主要代碼

store.js

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
    state: {
        catch_components: [],
        activePath: '/index',
        openNames: [],
        activeName: "",
        tranx: "-0",
        tabList: [
            { path: '/index', label: '首頁', name: '首頁' }
        ]
    },
    mutations: {
        //清空vuex數(shù)據(jù)
        clearTabs(state) {
            state.catch_components = []
            state.activePath = '/homepage'
            state.openNames = []
            state.activeName = ""
            state.tranx = "-0"
            state.tabList = [
                { path: '/homepage', label: '首頁', name: 'home' }
            ]
        },
        // 跳轉頁面執(zhí)行
        selectMenu(state, submenu) {
            var activePath = submenu.path
            var oldTabList = state.tabList
            var result = oldTabList.some(item => {
                if (item.path === activePath) {
                    return true
                }
            })
            if (!result) {
                oldTabList.push({
                    path: submenu.path,
                    name: submenu.name,
                    label: submenu.label,
                    index: submenu.index,
                    subName: submenu.subName
                })
            }
            state.activePath = activePath
            state.tabList = oldTabList
            state.activeName = submenu.subName + "-" + submenu.index
            state.openNames = [submenu.subName]
        },
        // 添加keepalive緩存
        addKeepAliveCache(state, val) {
            if (val === '/homepage') {
                return
            }
            if (state.catch_components.indexOf(val) === -1) {
                state.catch_components.push(val)
            }
        },
        // 刪除keepalive緩存
        removeKeepAliveCache(state, val) {
            let cache = state.catch_components
            for (let i = 0; i < cache.length; i++) {
                if (cache[i] === val) {
                    cache.splice(i, 1);
                }
            }
            state.catch_components = cache
        },
        setTranx(state, val) {
            console.log(val)
            state.tranx = val
        },
        //關閉菜單
        closeTab(state, val) {
            state.activePath = val.activePath
            state.tabList = val.tabList
            state.openNames = val.openNames
            state.activeName = val.activeName
        },
        // 點擊標簽選擇菜單
        changeMenu(state, val) {
            state.activePath = val.path
            state.activeName = val.subName + "-" + val.index
            state.openNames = [val.subName]
        }
    },
})

頁面代碼

computed: {
  ...mapState({
  activePath: (state) => state.activePath, // 已選中菜單
    tabList: (state) => state.tabList, // tags菜單列表
    catch_components: (state) => state.catch_components, // keepalive緩存
    openNames: (state) => state.openNames,
    activeName: (state) => state.activeName,
    tranx: (state) => state.tranx,
  }),
},
watch: {
   openNames() {
     this.$nextTick(() => {
       this.$refs.asideMenu.updateOpened();
     });
   },
   activeName() {
     this.$nextTick(() => {
       this.$refs.asideMenu.updateActiveName();
     });
   },
 },
handleClose(tab, index) {
  var oldOpenNames = this.$store.state.openNames,
    oldActiveName = this.$store.state.activeName,
    oldActivePath = this.$store.state.activePath,
    oldTabList = this.$store.state.tabList;
  let length = oldTabList.length - 1;
  for (let i = 0; i < oldTabList.length; i++) {
    let item = oldTabList[i];
    if (item.path === tab.path) {
      oldTabList.splice(i, 1);
    }
  }
  // 刪除keepAlive緩存
  this.$store.commit("removeKeepAliveCache", tab.path);
  if (tab.path !== oldActivePath) {
    return;
  }
  if (length === 1) {
    this.$store.commit("closeTab", {
      activePath: "/index",
      tabList: oldTabList,
    });
    this.$router.push({ path: oldTabList[index - 1].path });
    return;
  }
  if (index === length) {
    oldActivePath = oldTabList[index - 1].path;
    oldOpenNames = [oldTabList[index - 1].subName];
    oldActiveName =
      oldTabList[index - 1].subName + "-" + oldTabList[index - 1].index;
    this.$store.commit("closeTab", {
      activePath: oldActivePath,
      tabList: oldTabList,
      openNames: oldOpenNames,
      activeName: oldActiveName,
    });
    this.$router.push({ path: oldTabList[index - 1].path });
  } else {
    oldActivePath = oldTabList[index].path;
    oldOpenNames = [oldTabList[index].subName];
    oldActiveName =
      oldTabList[index].subName + "-" + oldTabList[index].index;
    this.$store.commit("closeTab", {
      activePath: oldActivePath,
      tabList: oldTabList,
      openNames: oldOpenNames,
      activeName: oldActiveName,
    });
    this.$router.push({ path: oldTabList[index].path });
  }
  this.getTrans(2);
},
changeMenu(item) {
  var oldActivePath = this.$store.state.activePath;
  if (oldActivePath === item.path) {
    return;
  }
  this.$store.commit("changeMenu", item);
  this.$router.push({ path: item.path });
  this.$nextTick(() => {
    this.getTrans(0);
  });
},
selectMenu(item, i, subName) {
  // 加入keepalive緩存
  this.$store.commit("addKeepAliveCache", item.path);
  var submenu = {
    path: item.path,
    name: item.title,
    label: item.title,
    index: i,
    subName: subName,
  };
  this.$store.commit("selectMenu", submenu);
  this.$router.push({ path: item.path });
  this.$nextTick(() => {
    this.getTrans(0);
  });
},
getTrans(e) {
  let width = 0;
  if (this.$refs.tags) {
    width = this.$refs.tags.clientWidth;
  }
  this.tabList.map((item, index) => {
    if (item.path === this.activePath) {
      this.currentIndex = index;
    }
    if (this.$refs[`tag${index}`] && this.$refs[`tag${index}`][0]) {
      this.$set(
        this.tabList[index],
        "width",
        this.$refs[`tag${index}`][0].$el.clientWidth + 4
      );
    }
  });
  let list = this.tabList.filter((item, index) => {
    return index <= this.currentIndex;
  });
  let totalWidth = list.reduce((total, currentValue) => {
    return total + Number(currentValue.width);
  }, 0);
  let totalAllWidth = this.tabList.reduce((total, currentValue) => {
    return total + Number(currentValue.width);
  }, 0);
  if (e == 0) {
    if (Number(width) > Number(totalWidth) || Number(width) == 0) {
      this.setTranx(-0);
      return false;
    }
    this.setTranx(Number(width) - Number(totalWidth) - 60);
  } else if (e == 1) {
    if (Number(width) > Number(totalAllWidth)) {
      return false;
    }
    this.setTranx(Number(width) - Number(totalAllWidth) - 60);
  } else {
    if (
      Number(width) > Number(totalAllWidth) &&
      this.$store.state.tranx < 0
    ) {
      this.setTranx(-0);
    }
  }
},
setTranx(val) {
  this.$store.commit("setTranx", val);
},
<Menu
  ref="asideMenu"
  :active-name="activeName"
  :open-names="openNames"
  accordion
  theme="light"
  :style="{ width: 'auto' }"
  :class="isCollapsed ? 'collapsed-menu' : 'menu-item'"
>
  <MenuItem
    @click.native="selectMenu({ path: '/index', title: '首頁' })"
    name="index"
    key="index"
  >
    <Icon type="ios-paw"></Icon>
    <span class="menuTitle">首頁</span>
  </MenuItem>
  <Submenu
    v-for="(item, index) in menuMap"
    :name="index"
    :key="index"
    class="sub_title"
  >
    <template slot="title">
      <svg class="icon" aria-hidden="true" v-if="item.fonticon">
        <use :xlink:href="item.fonticon" rel="external nofollow" ></use>
      </svg>
      <Icon :type="item.icon" v-else />
      <span class="menuTitle">{{ item.title }}</span>
    </template>
    <template v-if="item.children">
      <MenuItem
        v-for="(each, i) in item.children"
        :name="index + '-' + i"
        :key="index + '-' + i"
        @click.native="selectMenu(each, i, index)"
        ><span class="menuTitle">{{ each.title }}</span>
      </MenuItem>
    </template>
  </Submenu>
</Menu>
<Row class="head-tags">
  <div class="head-left left" @click="setTranx(0)">
    <Icon type="ios-rewind" size="30" color="#ffc0cb" />
  </div>
  <div class="tags-box">
    <div
      ref="tags"
      class="tags-box-scroll"
      :style="{ transform: `translateX(${tranx}px)` }"
    >
      <Tag
        :ref="'tag' + index"
        class="tags-item"
        :class="{ 'tags-item-active': activePath === item.path }"
        v-for="(item, index) in tabList"
        :key="index"
        :name="item.path"
        :closable="item.path !== '/index'"
        @click.native="changeMenu(item)"
        @on-close="handleClose(item, index)"
        >{{ item.label }}</Tag
      >
    </div>
  </div>
  <div class="head-left right" @click="getTrans(1)">
    <Icon type="ios-fastforward" size="30" color="#ffc0cb" />
  </div>
</Row>

總結 

到此這篇關于vue實現(xiàn)多個tab標簽頁的切換與關閉的文章就介紹到這了,更多相關vue多個tab標簽頁切換關閉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 在Vue中如何實現(xiàn)打字機的效果

    在Vue中如何實現(xiàn)打字機的效果

    這篇文章主要介紹了在Vue中如何實現(xiàn)打字機的效果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue中使用$http.post請求傳參的錯誤及解決

    vue中使用$http.post請求傳參的錯誤及解決

    這篇文章主要介紹了vue中使用$http.post請求傳參的錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue2無法監(jiān)聽數(shù)組下標和對象新增屬性的原因和解決方法

    Vue2無法監(jiān)聽數(shù)組下標和對象新增屬性的原因和解決方法

    文章詳細解釋了Vue2中無法監(jiān)聽對象屬性新增/刪除和數(shù)組下標變化的原因,并提供了解決方案,如使用Vue.set和重寫數(shù)組方法,還對比了Vue3的Proxy,強調了Proxy在監(jiān)聽機制和性能上的優(yōu)勢,需要的朋友可以參考下
    2026-05-05
  • 用vue的雙向綁定簡單實現(xiàn)一個todo-list的示例代碼

    用vue的雙向綁定簡單實現(xiàn)一個todo-list的示例代碼

    本篇文章主要介紹了用vue的雙向綁定簡單實現(xiàn)一個todo的示例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • vue+element樹形選擇器組件封裝和使用方式

    vue+element樹形選擇器組件封裝和使用方式

    這篇文章主要介紹了vue+element樹形選擇器組件封裝和使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2020-04-04
  • vue.js中Vue-router 2.0基礎實踐教程

    vue.js中Vue-router 2.0基礎實踐教程

    這篇文章主要給大家介紹了關于vue.js中Vue-router 2.0基礎實踐的相關資料,其中包括vue-router 2.0的基礎用法、動態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關知識,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • Vue--Router動態(tài)路由的用法示例詳解

    Vue--Router動態(tài)路由的用法示例詳解

    這篇文章主要介紹了Vue--Router動態(tài)路由的用法,很多時候,我們需要將給定匹配模式的路由映射到同一個組件,在?Vue?Router?中,我們可以在路徑中使用一個動態(tài)字段來實現(xiàn),我們稱之為路徑參數(shù),本文對Vue?Router動態(tài)路由相關知識給大家介紹的非常詳細,需要的朋友參考下吧
    2022-08-08
  • vue頁面跳轉過渡動畫與防止抖動方式

    vue頁面跳轉過渡動畫與防止抖動方式

    這篇文章主要介紹了vue頁面跳轉過渡動畫與防止抖動方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • 基于vue3實現(xiàn)一個簡單的輸入框效果

    基于vue3實現(xiàn)一個簡單的輸入框效果

    這篇文章主要為大家詳細介紹了如何使用Vue3實現(xiàn)一個簡單的輸入框,可以實現(xiàn)輸入文字,添加表情等功能,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • uni app仿微信頂部導航條功能

    uni app仿微信頂部導航條功能

    這篇文章主要介紹了uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09

最新評論

凉城县| 台中市| 尼玛县| 新郑市| 寿阳县| 如皋市| 永定县| 普兰店市| 九江市| 当涂县| 永年县| 金沙县| 钟山县| 临沧市| 铜鼓县| 清水县| 林周县| 苏尼特左旗| 廊坊市| 简阳市| 行唐县| 永新县| 民勤县| 阿拉善右旗| 玛多县| 磐石市| 洛宁县| 海原县| 广平县| 闽清县| 阳山县| 鹿泉市| 黑河市| 南靖县| 襄樊市| 康定县| 文山县| 子洲县| 洱源县| 仲巴县| 石渠县|