Skip to content

Commit

Permalink
feat: 页面根据路由 title 设置标签名
Browse files Browse the repository at this point in the history
  • Loading branch information
yulimchen committed Feb 13, 2023
1 parent e097c1f commit caff118
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
} from "vue-router";
import routes from "./routes";
import { useCachedViewStoreHook } from "@/store/modules/cachedView";
import NProgress from "@/utils/progress";
import setPageTitle from "@/utils/set-page-title";

const router = createRouter({
history: createWebHashHistory(),
Expand All @@ -19,9 +21,16 @@ export interface toRouteType extends RouteLocationNormalized {
}

router.beforeEach((to: toRouteType, from, next) => {
NProgress.start();
// 路由缓存
useCachedViewStoreHook().addCachedView(to);
// 页面 title
setPageTitle(to.meta.title);
next();
});

router.afterEach(() => {
NProgress.done();
});

export default router;
3 changes: 3 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg from "../package.json";

export const pageDefaultTitle = pkg.name || "Vue3 H5 Template";
7 changes: 7 additions & 0 deletions src/utils/set-page-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { pageDefaultTitle } from "@/settings";

export default function setPageTitle(routerTitle?: string): void {
window.document.title = routerTitle
? `${routerTitle} | ${pageDefaultTitle}`
: `${pageDefaultTitle}`;
}

0 comments on commit caff118

Please sign in to comment.