vue路由跳转页面title随之变化

一、通过路由钩子

1.每个路由添加一个meta

[
  {
     path:'/index',
     component:'Index',
     meta: {
        title: '首页'
     },
   }
]

2.main.js文件加入钩子函数

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  methods: {
    Ajax,
  }
})

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

二、 Vue.js单页应用在iOS系统下部分APP的webview中,标题不能通过document.title = xxx 的方式修改,该插件只为解决该问题而生(兼容安卓)

https://www.npmjs.com/package/vue-wechat-title

1.安装

Vue.js 1.x
npm install vue-wechat-title@1.0.7 --save

Vue.js 2.x
npm install vue-wechat-title --save

2.引入

在main.js文件中引入
ES6:Vue.use(require('vue-wechat-title'))
或
import WechatTitle from 'vue-wechat-title'
Vue.use(WechatTitle)

3.用法

router中每个路由添加meta
[
  {
     path:'/index',
     component:'Index',
     meta: {
        title: '首页'
     },
   }
]
<!-- 任意元素中加 v-wechat-title 指令 建议将标题放在 route 对应meta对象的定义中 -->
<div v-wechat-title="$route.meta.title"></div>
<!--or-->
<router-view v-wechat-title="$route.meta.title"></router-view>
自定义加载的图片地址 默认是 ./favicon.ico 可以是相对或者绝对的
<div v-wechat-title="$route.meta.title" img-set="/static/logo.png"></div>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值