关于 router-view-slot

关于 router-view-slot

App.vue
<template>
    <router-view></router-view>
</template>
配置动态路由
import layout from "@/layout/index.vue";
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            name: "layout",
            redirect: "/dashboard",
            component: layout,
        },
    ]
});
const routes = [
    {
        path: "/help-center",
        name: "helpCenter",
        meta: {
            title: "帮助中心",
        },
        component: () => import("@/views/HelpCenter.vue"),
    },
    {
        path: "/dashboard",
        name: "dashboard",
        meta: {
            title: "仪表盘",
        },
        component: () => import("@/views/Dashboard.vue"),
    },
    {
        path: "/analytics",
        name: "analytics",
        meta: {
            title: "数据分析",
        },
        component: () => import("@/views/Analytics.vue"),
    },
    {
        path: "/user-profile",
        name: "userProfile",
        meta: {
            title: "用户资料",
        },
        component: () => import("@/views/Profile.vue"),
    },
    {
        path: "/project-management",
        name: "projectManagement",
        meta: {
            title: "项目管理",
        },
        component: () => import("@/views/Management.vue"),
    },
];

routes.forEach((v) => router.addRoute("layout", v));

export default router;

添加嵌套路由,这里为 layout 添加子路由,
addRoute("layout", v)) 相当于

router.addRoute({
    name: "layout",
    path: "/layout",
    component: layout,
    children: [{ path: "/help-center", component: () => import("@/views/HelpCenter.vue") }],
});
layout.vue
<template>
    <div class="layout">
        <div class="left">
            <div
                v-for="(item, index) in routes"
                :key="index">
                <div>
                    <router-link :to="item.path">
                        {{ item?.meta?.title || "首页" }}
                    </router-link>
                </div>
            </div>
        </div>
        <div class="right">
            <router-view v-slot="{ Component }">
                <component
                    :is="Component"
                    :key="route.path" />
            </router-view>
        </div>
    </div>
</template>

<script lang="ts" setup>
    import { useRoute, useRouter } from "vue-router";
    const route = useRoute();
    const router = useRouter();
    const routes = router.getRoutes();
</script>

使用 router-view v-slot 进行动态展示,transitionkeep-alive 现在必须通过 v-slot 在 RouterView 内部使用

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

链接: router-view-keep-alive-transitions.md

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今晚也失眠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值