HLJ 发布于
2018-08-16 17:57:52

使用vue-router为每个路由配置各自的title值

使用Vue-Router的方法
首先打开/src/router/index.js文件。
找到如下代码。
const vueRouter = new Router({
  routes,
  mode: 'history',
  linkActiveClass: 'active-link',
  linkExactActiveClass: 'exact-active-link',
  scrollBehavior (to, from, savedPosition) {
   if (savedPosition) {
    return savedPosition;
   } else {
    return { x: 0, y: 0 };
   }
  },
 });
 vueRouter.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
   document.title = to.meta.title;
  }
  next();
 });
代码的逻辑就是在路由将要发生变化的时候,用传统的方法来对每个将要跳转到的路由的title进行修改。
配置路由
配置好了index.js之后我们就需要去给每个router配置自己的title了。例如。
{
 path: '/',
 name: 'Home',
 component: () => import('@/views/Home/Home'),
 meta: {
  title: '首页',
 },
}
给每个路由加上一个叫meta的属性。meta属性里的属性叫title,也就是每个路由独特的title了。加上之后,浏览器里每个路由都会有自己设置好的title了。
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-08-16/108.html
最后生成于 2023-08-04 21:34:40
此内容有帮助 ?
0