微前端兩個子應(yīng)用加tab傲隶,做到菜單切換

業(yè)務(wù)場景

微前端子應(yīng)用間加tab,做到菜單切換

效果

類似效果

涉及問題

因?yàn)檫@是加在每個子應(yīng)用間窃页,所以需要在每個子應(yīng)用都加上tabs組件跺株,切換的菜單如何在每個子應(yīng)用更新?

處理方案

思路就是將我們點(diǎn)擊的菜單保存到localStorage中脖卖,然后每個子應(yīng)用去監(jiān)聽localStorage值的變化乒省,從而更新tabs組件的值。

在資源文件中新建locale.js

export default function dispatchEventStroage () {
    let signSetItem = localStorage.setItem
    localStorage.setItem = function (key, val) {
      let setEvent = new Event('setItemEvent')
      setEvent.key = key
      setEvent.newValue = val
      window.dispatchEvent(setEvent)
      signSetItem.apply(this, arguments)
    }
  }

在index.js中引用這個js畦木,我這里是放在plugins文件夾下

import tool from '@/plugins/locale';
Vue.use(tool);

在App.vue加上tabs這個組件

<template>
  <div class="app" @click="down">
    <!-- 加載tabs組件 -->
    <a-tabs
      v-model="activeKey"
      type="editable-card"
      hideAdd
      @edit="onEdit"
      @change="onChange"
      @contextmenu.prevent="open($event)"
    >
      <a-tab-pane
        v-for="pane in initalItem"
        :key="pane.key"
        :tab="pane.title"
        :closable="pane.closable"
      >
        <!-- 這里做的是自定義右鍵 -->
        <ul
          v-show="visible"
          :style="{ left: left + 'px', top: top + 'px' }"
          class="newcontextmenu"
        >
          <li @click="close">關(guān)閉標(biāo)簽</li>
          <li @click="closeAll">關(guān)閉所有標(biāo)簽</li>
        </ul>
      </a-tab-pane>
    </a-tabs>
      <div class="app-main">
          <router-view />
        </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeKey: this.$route.path,
      initalItem: JSON.parse(localStorage.getItem("initalItem")),
      visible: false,
      top: 0,
      left: 0,
    };
  },
  created() {},
  mounted() {
    let that = this;
    // 監(jiān)聽localStorage的值的變化
    this.$nextTick(function () {
      window.addEventListener("setItemEvent", (e) => {
        setTimeout(function () {
          that.initalItem = JSON.parse(localStorage.getItem("initalItem"));
        });
        setTimeout(function () {
          that.activeKey = that.$route.path;
        });
      });
    });
  },
  methods: {
    onEdit(targetKey, action) {
      if (action == "remove") {
        const initalItem = JSON.parse(localStorage.getItem("initalItem"));
        initalItem.splice(
          initalItem.findIndex((item) => item.key === targetKey),
          1
        );
        localStorage.setItem("initalItem", JSON.stringify(initalItem));
        this.initalItem = initalItem;
        this.activeKey = initalItem[initalItem.length - 1].key;
        this.$router.push(this.activeKey);
      }
    },
    onChange(targetKey) {
      const initalItem = JSON.parse(localStorage.getItem("initalItem"));
      let data = initalItem.find((item) => item.key == targetKey);
      this.$router.push({
        path: targetKey,
        query: data.query,
      });
    },
    open(e) {
      this.visible = false;
      var x = e.pageX + 50;
      var y = e.pageY;
      this.top = y;
      this.left = x;
      this.visible = true;
    },
    down() {
      this.visible = false;
    },
    close() {
      const initalItem = JSON.parse(localStorage.getItem("initalItem"));
      initalItem.splice(
        initalItem.findIndex((item) => item.key === this.activeKey),
        1
      );
      localStorage.setItem("initalItem", JSON.stringify(initalItem));
      this.initalItem = initalItem;
      this.activeKey = initalItem[initalItem.length - 1].key;
      this.$router.push(this.activeKey);
    },
    closeAll() {
      localStorage.setItem(
        "initalItem",
        JSON.stringify([
          {
            title: "首頁",
            key: "/index",
            closable: false,
          },
        ])
      );
      this.initalItem = [
        {
          title: "首頁",
          key: "/index",
          closable: false,
        },
      ];
      this.activeKey = "/index";
      this.$router.push("/index");
    },
  },
};
</script>
<style lang="less">
.newcontextmenu {
  position: fixed;
  border: 1px solid #ccc;
  border-radius: 5px;
  background: #fff;
  li {
    padding: 10px;
    cursor: pointer;
  }
}
</style>

修改router.js

//去重袖扛,后面點(diǎn)擊的覆蓋前面點(diǎn)擊的(對應(yīng)的key值)
function deWeight(arr) {
  let map = new Map();
  for(let item of arr){
    map.set(item.key,item)
  }
  return [...map.values()];
}
router.beforeEach(async (to, from, next) => {
  let array = JSON.parse(localStorage.getItem("initalItem"))?JSON.parse(localStorage.getItem("initalItem")):[]
  if(to.path === '/index'){
    array.push({
      title: '首頁',
      key: to.path,
      closable: false
    })
  }else{
    array.push({
      title: to.meta.title,
      key: to.path,
      query: to.query
    })
  }
  localStorage.setItem("initalItem",JSON.stringify(deWeight(array)));
});

這是其中一個子應(yīng)用的配置,另一個子應(yīng)用也是相對應(yīng)的配置馋劈。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末攻锰,一起剝皮案震驚了整個濱河市晾嘶,隨后出現(xiàn)的幾起案子妓雾,更是在濱河造成了極大的恐慌,老刑警劉巖垒迂,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械姻,死亡現(xiàn)場離奇詭異,居然都是意外死亡机断,警方通過查閱死者的電腦和手機(jī)楷拳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吏奸,“玉大人欢揖,你說我怎么就攤上這事》芪担” “怎么了她混?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長泊碑。 經(jīng)常有香客問我坤按,道長,這世上最難降的妖魔是什么馒过? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任臭脓,我火速辦了婚禮,結(jié)果婚禮上腹忽,老公的妹妹穿的比我還像新娘来累。我一直安慰自己砚作,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布嘹锁。 她就那樣靜靜地躺著偎巢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪兼耀。 梳的紋絲不亂的頭發(fā)上压昼,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天,我揣著相機(jī)與錄音瘤运,去河邊找鬼窍霞。 笑死,一個胖子當(dāng)著我的面吹牛拯坟,可吹牛的內(nèi)容都是我干的但金。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼郁季,長吁一口氣:“原來是場噩夢啊……” “哼冷溃!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起梦裂,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤似枕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后年柠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凿歼,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年冗恨,在試婚紗的時候發(fā)現(xiàn)自己被綠了答憔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡掀抹,死狀恐怖虐拓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情傲武,我是刑警寧澤蓉驹,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站谱轨,受9級特大地震影響戒幔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜土童,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一诗茎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦敢订、人聲如沸王污。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昭齐。三九已至,卻和暖如春矾柜,著一層夾襖步出監(jiān)牢的瞬間阱驾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工怪蔑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留里覆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓缆瓣,卻偏偏與公主長得像喧枷,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子弓坞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容