VUE动态修改titile的三种方法

摘要:
<template><div><h1>{{title}}}}</h1></div></template><script>exportdefault{name:‘post‘,data(){return{title:‘description:‘This is a article…‘},metaInfo()}{return{title:This.title,meta:〔{vmid:‘description‘,name:‘description’,content:This.description}〕},created(){This.initData()},方法:{initData(){axios.get.then}}第三种类型:vue-wechat title 3.1安装npminstallvue-wechat title--save3.2从“vue-wechant title”vue引入importVueWechat title。use3.3在router˃索引中添加元对象。js配置titleconstructer=newRouter;路线afterEach(route=˃{//获取title属性if(route.meta.title){document.title=route.meta.title;//对于iOS设备,使用以下黑客更新页面标题if(navigator.userAgent.match(/(i[^;]+;(U;)?

第一种:适用于在已经定义好title的情况下,例如首页,关于页等等

1.1 main.js

const defaultTitle = '默认 title'
router.beforeEach((to, from, next) => {
 document.title = to.meta.title ? to.meta.title : defaultTitle
 next()
})

1.2 index.js

routes: [
    {
        name:'home',
          path: '/home/:openname',
          component: Home,
          meta: {
            title: '首页'
        }
    }
  ]

第二种:vue-meta 插件(适用于无法固定title的情况下,例如文章页)

vue-meta官网文档看这里

2.1 安装

npm install vue-meta --save

2.2 在main.js引入

import Meta from 'vue-meta'
Vue.use(Meta)

2.3 为需要修改的页面单独定义metaInfo

export default {
    metaInfo: {
      title: 'This is the test',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=2,user-scalable=yes' }
      ]
    }
}

2.4 异步请求数据可以使用

如果component中使用了异步请求数据,可以使用 metaInfo() 方法。

<template>
  <div>
    <h1>{{{ title }}}</h1>
  </div>
</template>

<script>
  export default {
    name: 'post',
    data () {
      return {
        title: ''
        description: '这是一篇文章...'
      }
    },
    metaInfo () {
      return {
        title: this.title,
        meta: [
          { vmid: 'description', name: 'description', content: this.description }
        ]
      }
    },
    created () {
      this.initData()
    },
    methods: {
      initData () {
        axios.get('some/url').then((resp) => {
          // 设置title时 metaInfo 会同时更新
          this.title = resp.title
          this.description = resp.decription
        })
      }
    }
  }
</script>

第三种:vue-wechat-title

3.1 安装

npm install vue-wechat-title --save

3.2 在main.js中引入

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

3.3 使用

在router>index.js中添加meta对象配置title

const router = new Router({
 
  routes: [
    ...
    {
      path: "/gameDesc", 
      name: 'gameDesc',
      component: resolve => import('@/pages/Game/gameDesc'),
      meta:{
        title: '游戏说明'
      }
    },
    {
      path: "/integralList", 
      name: 'integralList',
      component: resolve => import('@/pages/Game/integralList'),
      meta:{
        title: '积分收取记录'
      }
    }
    ...
 
 ]
});
 
router.afterEach(route => {
  // 从路由的元信息中获取 title 属性
  if (route.meta.title) {
    document.title = route.meta.title;
    // 如果是 iOS 设备,则使用如下 hack 的写法实现页面标题的更新
    if (navigator.userAgent.match(/(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      const hackIframe = document.createElement('iframe');
      hackIframe.style.display = 'none';
      hackIframe.src = 'http://t.zoukankan.com/static/html/fixIosTitle.html?r=' + Math.random();
      document.body.appendChild(hackIframe);
      setTimeout(_ => {
        document.body.removeChild(hackIframe)
      }, 300)
    }
  }
});
 
export default router;

  

在App.vue中修改router-view

<router-view v-wechat-title='$route.meta.title'></router-view>

免责声明:文章转载自《VUE动态修改titile的三种方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇sql 简单查询修改python 删除多个同一后缀名文件(基于python 3.X)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决vscode格式化vue文件出现的问题

遇到的问题 使用vscode开发vue项目的时候,格式化vue文件,与自己配置的eslint标准会有冲突。 引号问题:单引号变双引号 分号问题:行末是否加分号。自动加/减分号 当然还会有其他个性化冲突,只需要找到怎么修改,就好办了。 说明 vscode格式化文件,使用的是快捷键shift + alt + f 而我安装了vetur插件来格式化vue文件。...

vue中的父子组件之间的通信--新增、修改弹框

在一个vue页面中有时候内容会很多,为了方便编写查看,可以分为多个子组件,最后在父组件中引入对应的子组件即可。 下面这个是父子组件通信中的一个具体实例:新增、修改弹框。子组件中主要写了关于新增、修改的弹框, 子组件: 1.弹框: <div class="newDocuments"> <div class="newDocuments_...

修改ElementUI的样式----vue如何控制步骤条steps圆圈的大小 data-v-

1、问题 使用 vue 时写发现某些样式不生效,怎么都不生效, 不过将style 中的 scoped 去掉后,居然生效了。但是一般都应该加上scoped,那该如何处理呢? <template> <div class="app-container"> <heads /> <div c...

前端框架Vue自学之Vue CLI(五)

终极目标:掌握和使用Vue(全家桶:Core+Vue-router+Vuex) 本博客目的:记录Vue学习的进度和心得(Vue CLI) 内容:学习和使用Vue CLI2 和 Vue CLI3。如果了解webpack如何一步步配置的,建议可以先看我的前一个博客:前端框架Vue自学之webpack(四)。 正文: Vue CLI 一、Vue CLI 1、前言...

vue中handsontable 使用

handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作 1.安装模块包 npm install handsontable-pro @handsontable-pro/vue npm install handsontable @handsontable/vue 这样安装完handsontable依...

vue中Axios的封装和API接口的管理

前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习。如果对原博主造成侵犯,我会立即删除。 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题,我们所要的说的axios查看更多关于 axios 的文章 的封装和api接口的统一管理,其实主要目的就是在帮助我们简化代码和利于后期的更新维护。 一、ax...