效果图如下
首先看我的目录结构(用的是 vue3)
路由的配置:
import {
createRouter, createWebHistory } from "vue-router";
const Home = () => import("@/views/Home/Home");
const Category = () => import("@/views/Category/Category");
const ShopCart = () => import("@/views/ShopCart/ShopCart");
const Profile = () => import("@/views/Profile/Profile");
const routes = [
{
path: "",
redirect: "/home",
},
{
path: "/home",
name: "Home",
component: Home,
meta: {
title: "首页",
},
},
{
path: "/category",
name: "Category",
component: Category,
meta: {
title: "分类",
},
},
{
path: "/shopcart",
name: "ShopCart",
component: ShopCart,
meta: {
title: "购物车",
},
},
{
path: "/profile",
name: "Profile",
component: Profile,
meta: {
title: "个人主页",
},
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
});
// 动态改变 title
router.beforeEach((to, from, next) => {
document.title = to.meta.title;
next();
}