vue3+ts+setup 路由中的路由保留原来的位置以及参数

app.vue

	
<template>
  <div class="zjl-pc-vue3-typeScript">

    <!-- <keep-alive>
      <router-view></router-view>

      
    </keep-alive> -->

    <router-view v-slot="{ Component }">
      <keep-alive>
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </div>
</template>

index.vue


	
<template>
  <section class="steward_container">
    <header class="steward_header">
      <normal-header />
    </header>
    <section class="steward_contect">
      <aside class="steward_aside">
        <normal-menu />
      </aside>
      <main class="steward_main">
        <!-- <top-tabs></top-tabs> -->
        <tab-close />  // 面包削
        <div class="steward_main_router">
          <!-- <router-view v-slot="{ Component, route }">
            <keep-alive v-if="route.meta.keepAlive">
              <component :is="Component" :key="route.name" />
            </keep-alive>
            <component v-else :is="Component" :key="route.name" />
          </router-view> -->
          <!-- <router-view></router-view> -->

          <router-view v-slot="{ Component }">
            <keep-alive>
              <component :is="Component" />
            </keep-alive>
          </router-view>
        </div>
      </main>
    </section>
  </section>
</template>


router index.ts

	// 全局守卫
router.beforeEach((to, from, next) => {
  console.log(to, from, next);

  const counter = useCounterStore();
  const { allMenu } = storeToRefs(counter);
  const { changeTab, updateTopTabs } = counter;
  // 未登录跳转登录界面
  // const isAuthenticated = localStorage.getItem("user_id");
  const isAuthenticated = sessionStorage.getItem("user_id");
  if (to.name !== "login" && !isAuthenticated) {
    next({ name: "login" });
  } else {
    // 根据路由切换选中项
    const path = to.path.split("/");
    // console.log('切换路由');
    changeTab(path[2]);
    // 如果不存在该tab需添加
    for (const tab of allMenu.value) {
      if (path[2] === tab.name) {
        updateTopTabs({
          icon: tab.icon,
          label: tab.label,
          name: tab.name,
          path: tab.path,
          sort: tab.sort,
          path2: to.fullPath,
        });
        console.log(tab);
      }
    }
    console.log(path);

    console.log(window.location.href.split("#"));
    console.log(window.location.hash);

    next();
  }
});


store pinia.ts



  // 更新tabs
  function updateTopTabs(data) {
    // // 没有该数据才添加
    // console.log(data);
    // console.log(topTabs.value);
    // topTabs.value.map(item=>{
    //   if(item.name === data.name){
    //     item.path = data.path
    //   }

    // })
    // console.log(topTabs.value);

    // for (const tab of topTabs.value) {
    //   // if (tab.path === data.path) return false;
    //   if (tab.name === data.name) return false;
    // }
    // console.log(data);

    // topTabs.value.push(data);
    // // topTabs.value.push(data);
    // sessionStorage.setItem("topTabs", JSON.stringify(topTabs.value));

    console.log(data);
    console.log(topTabs.value);
    topTabs.value.map((item) => {
      if (item.name == data.name) {
        item.path2 = data.path2;
      }
    });
    for (const tab of topTabs.value) {
      // if (tab.path === data.path) return false;
      if (tab.name === data.name) return false;
    }

    topTabs.value.push(data);
    console.log(topTabs.value);
    sessionStorage.setItem("topTabs", JSON.stringify(topTabs.value));
  }

  // 删除tabs
  function deleteTopTabs(selectName: string | number) {
    topTabs.value = topTabs.value.filter((tab) => tab.name !== selectName);
    sessionStorage.setItem("topTabs", JSON.stringify(topTabs.value));
  }
  // 清空tabs
  function clearTopTabs() {
    topTabs.value = [
      {
        label: "首页",
        name: "home",
        path: "/index",
      },
    ];
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跟着阿龙学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值