南强小屋 Design By 杰米

本文主要介绍了Vue动态修改title的方法,需要的朋友可以参考学习,方法如下:

1.通过自定义指令去修改(单个修改比较好)

//1.在main.js 页面里添加自定义指令
Vue.directive('title', {//单个修改标题
 inserted: function (el, binding) {
 document.title = el.dataset.title
 }
})
//2.在需要修改的页面里添加v-title 指令
<div v-title data-title="我是新的标题"></div>.

2.使用插件 vue-wechat-title

//1.安装插件
npm vue-wechat-title --save
//2.在main.js里面引入插件
import VueWechatTitle from 'vue-wechat-title'//动态修改title
Vue.use(VueWechatTitle)
//3.在路由里面 添加title
   routes: [{
   path: '/login',
   component: Login,
   meta: {
    title: '登录'
   }
  }, {
   path: '/home',
   component: Home,
   meta: {
    title: '首页'
   }
  },]

//4.在app.vue 中添加 指令 v-wechat-title
<router-view v-wechat-title='$route.meta.title' />

3.通过 router.beforeEach 导航守卫来修改

//1.在router的index里写自己的路径和title
const router = new Router({
 routes: [
  {
   path: '/login',
   component: Login,
   meta: {
    title: '登录',
   },
  },
  {
   path: '/home',
   component: Home,
   meta: {
    title: '首页',
   },
  },
 ],
})
//2.使用router.beforeEach对路由进行遍历,设置title
router.beforeEach((to, from, next) => {
 //beforeEach是router的钩子函数,在进入路由前执行
 if (to.meta.title) {
  //判断是否有标题
  console.log(to.meta.title)
  document.title = to.meta.title
 } else {
  document.title = '默认title'
 }
 next()
})

4.使用 vue-mate 修改 title

https://github.com/declandewet/vue-meta 文档中比较详细地说明了在浏览器端和服务器端如何使用 vue-meta 修改页面头部信息

总结

标签:
vue设置title,vue动态修改title,vue在片头加标题

南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米

评论“vue动态设置页面title的方法实例”

暂无vue动态设置页面title的方法实例的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。