由於每個路由頁面的title可能都不一樣,因此使用document.title來進行動態的改變
1.在定義路由頁面的時候,寫入title名稱
const routes = [ { path: "/home", name: "home", component: () => import("../views/home.vue"), meta: { title: "首頁", }, }, ]
2.在main.js中使用路由守衛中的前置守衛(beforeEach)來實現title的動態改變
router.beforeEach(function (to, from, next) { if (to.meta.title) { document.title = to.meta.title; } next(); });