设置页面标题title


由于每个路由页面的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();
});

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM