如何給Vue项目中设置每个单页面的标题

如何給Vue项目中设置每个单页面的标题

第一种方法:直接在main.js之中加入路由导航守卫

第一步: 首先在路由文件index.js中给每个单页面路由添加title

routes: [{
    path: '/',
    name: 'index1',
    component: index1,
    meta:{
        title:'我是第一个标题'
    },
    {
    path: '/index2',
    name: 'index2',
    component: index2,
    meta:{
        title:'我是第二个标题'
    },
    ]

第二步:使用路由导航守卫


router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

第二种方法:使用vue-wechat-title插件

第一步: 首先在路由文件index.js中给每个单页面路由添加title

routes: [{
    path: '/',
    name: 'index1',
    component: index1,
    meta:{
        title:'我是第一个标题'
    },
    {
    path: '/index2',
    name: 'index2',
    component: index2,
    meta:{
        title:'我是第二个标题'
    },
    ]

第二步:下载安装依赖

 npm install vue-wechat-title --save-dev

第三步: 在main.js文件之中,导入并引用

import VueWechatTitle from 'vue-wechat-title'
Vue.use(VueWechatTitle)

第四步:在app.vue中修改

<router-view v-wechat-title='$route.meta.title' v-if='$route.meta.title'></router-view>
<router-view v-wechat-title='$route.meta.title' v-if='!$route.meta.title'></router-view>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值