vue实现路由跳转动态title标题信息

目录
  • 路由跳转动态title标题信息
  • vue动态改变title
    • 需求
    • 解决需求一
    • 解决需求二

路由跳转动态title标题信息

想要让浏览器的标题,随着vue的路由跳转的改变而改变,就要配置router/index.js文件里的信息。在meta对象里面配置一个title。

{
    path: "/",
    name: "Home",
    meta: {
      title: "首页"//这是重点
    },
    component: () => import( /* webpackChunkName: "about" */ "../views/home/index.vue"),
  }

然后在路由跳转之前判断跳转之后的页面是否配置的有title值。如果有则替换title,如果没有则保持title不变即可。

router.beforeEach((to, from, next) => {
  
  if (to.meta.title) { //如果设置标题,拦截后设置标题
    document.title = to.meta.title
  }
  })

配置成功之后,vue在进行路由跳转的时候,页面的title也会跟着路由配置的信息进行跳转。

--------2022-06-14补充--------

第二种方式:使用v-title

 <div class="wrapper" v-title :data-title="webTitle">
    <!-- <div class="wrapper-main-Img">
      <img
        src="../../../assets/images/mobile/hdkb.png"
        alt=""
        class="wrapper-main1-wqjm"
      />
    </div> -->
    <p class="hy-title">{{ columnName || "--" }}</p>
    <div class="warpper-news-list">
      <van-empty description="暂无数据" v-if="actLen == 0" />
      <div class="actLenWrap" v-if="actLen == 1">
        <div
          class="warpper-news-item"
          v-for="(i, v) in activetyList"
          :key="v"
          @click="toActDetails(i.id,i.title)"
        >
          <div class="warpper-news-l">
            <p class="warpper-news-title">{{ i.title }}</p>
            <p class="warpper-news-details">
              {{ i.description || '--' }}
            </p>
            <p class="warpper-news-time">{{ i.releaseTime.substring(0,10) || '--' }}</p>
          </div>
          <div class="warpper-news-r">
            <img
              src="../../../assets/images/mobile/indictor.png"
              alt=""
              class="wrapper-main1-indictor"
            />
          </div>
        </div>
      </div>
    </div>
    <van-pagination
      v-model="params.current"
      :page-count="Math.ceil(total / size)"
      mode="simple"
      @change="handleSize"
      v-if="actLen == 1"
      class="pageNation"
    />
  </div>
created() {
    this.columnName = this.$route.query.name;
    this.webTitle = this.columnName +'-test';
    this.params.columnId = this.$route.query.id;
    // this.getActList();
    this.contentPage();
  },

vue动态改变title

需求

1.不同路由路径下,动态更改title

2.相同路径下,像产品详情页,需要根据产品名字不同动态更改title

解决需求一

1.在router.js根据不同的路由配置所属title

{
    path: '/startCertificate',
    name: 'startCertificate',
    component: startCertificate,
    meta:{
      title:'这是动态路由哦'
    }
 },

2.在main.js中配置

情况一:普通h5开发

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

情况二:在app内嵌h5的混合应用中,iOS系统下部分APP的webview中的标题不能通过 document.title = xxx 的方式修改,因为在IOS webview中网页标题只加载一次,动态改变是无效的,解决代码如下

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 = '/static/html/fixIosTitle.html?r=' + Math.random();
      document.body.appendChild(hackIframe);
      setTimeout(_ => {
        document.body.removeChild(hackIframe)
      }, 300)
    }
  }
});

解决需求二

1.安装依赖:npm i vue-wechat-title

2.在main.js中配置:

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

3.在需要改变title的vue文件中:

<template>
    <div class="customerCaseDetail" v-wechat-title="changeTitle">
 
    </div>
</template>
<script>
export default {
    data() {
        return {
            changeTitle:''
        }
    },
    created() {
        this.changeTitle = '动态title'
    },
}
</script>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 详解vue 路由跳转四种方式 (带参数)

    1.  router-link 1. 不带参数 <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建议用name // 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始. 2.带参数 <router-link :to="{name:'home', para

  • vue实现动态路由详细

    目录 一.前端控制 1.在router.js文件(把静态路由和动态路由分别写在router.js) 2.store/permission.js(在vuex维护一个state,通过配角色来控制菜单显不显示) 3.src/permission.js 4.侧边栏的可以从vuex里面取数据来进行渲染 二.后端控制路由 1.store/permission.js,在vuex里面发送请求获取数据 2.整理一份数据结构,存到表里 3.写一个转化方法,把获取到的数据转换成router结构 主流的实现方式: 简单

  • vue 实现路由跳转时更改页面title

    一.router文件夹下的index文件中给每个path添加meta:{}: export default new Router({ routes: [ { path: '/', name: 'index', component: index, meta: { title: 'title1' } }, { path: '/studentInfo', name: 'studentInfo', component: studentInfo, meta: { title: 'title2' } } ]

  • vue实现路由跳转动态title标题信息

    目录 路由跳转动态title标题信息 vue动态改变title 需求 解决需求一 解决需求二 路由跳转动态title标题信息 想要让浏览器的标题,随着vue的路由跳转的改变而改变,就要配置router/index.js文件里的信息.在meta对象里面配置一个title. {     path: "/",     name: "Home",     meta: {       title: "首页"//这是重点     },     compone

  • 解决vue+router路由跳转不起作用的一项原因

    如下所示: Vue.use(Router) export default new Router({ mode:'history', routes: [ { path: '/', component: Login }, { path: '/login', component: Login }, { path: '/register',component: Register}, {path: '/*', component: NotFound}, ] }) 记得要写上 mode:'history',

  • vue相同路由跳转强制刷新该路由组件操作

    想必大家在平时开发的时候可能遇到这种需求,在打开该菜单页面的情况下,再次点击菜单需要刷新该组件(销毁再创建).而vue自身如果路由不变的情况下是不会这样做的,那么只能使用一些骚操作了. 1.在菜单的路由跳转上绑定一个随机query参数,例如时间戳或者随机数: this.$router.push({ path:"/xxx", query:{ t:Date.now(), }, }); 该操作会触发路由改变,但是组件内的状态没有初始化,因为组件没有被重建. 2.在路由容器上绑定key值: &

  • vue中路由跳转不计入history的操作

    我就废话不多说了,大家还是直接看代码吧~ <van-field label="选择部门" :value="arr.DepartMentName" readonly right-icon="arrow" @click="$router.replace({ name: 'tree' })" /> 在下个页面使用replace跳回来即可 补充知识:vue-router模式为history的项目打包发布后不能通过地址栏里的

  • vue router 路由跳转方法讲解

    目录 一.概述 二.跳转方法 1.使用router-link标签 2.使用router-replace 3.使用router-push 三.路由中params和query的区别 一.概述 使用到Vue的项目,我们最常见使用的就是Vue配套的Vue Router库. 那么在平日开发中,有多少种跳转路由的方法? 二.跳转方法 1.使用router-link标签 使用router-link标签,我们通常会使用到2个参数,最常用的就是to参数to参数,表示你想要跳转到的路由对象 router-link标

  • vue.js路由跳转详解

    本文为大家分享了vue.js路由的跳转,供大家参考,具体内容如下 1.路由demo示例 <div id="app"> <h1>Hello App!</h1> <p> <!-- 使用 router-link 组件来导航. --> <!-- 通过传入 `to` 属性指定链接. --> <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 --> <rout

  • Vue实现路由跳转至外界页面

    目录 Vue路由跳转至外界页面 解决办法 Vue路由跳转页面的几种方式 总结 Vue路由跳转至外界页面 用法 如果使用路由是在 vue 页面中来回跳转,可以使用 this.$router.push() 实现,但是如果想用这种方法跳转到外部链接就会报错,因为外部页面中是存在 HTTP 等前缀的. 解决办法 1. 在 data 中定义好要跳转的外部链接 data() {     return {         url: 'http://www.baidu.com'     } } 2. 按钮中创建

  • Vue实现路由跳转和嵌套

    一.配置 Router 用 vue-cli 创建的初始模板里面,并没有 vue-router,需要通过 npm 安装 cnpm i vue-router -D 安装完成后,在 src 文件夹下,创建一个 routers.js 文件,和 main.js 平级 然后在 router.js 中引入所需的组件,创建 routers 对象 import Home from './components/home.vue' const routers = [ { path: '/home', name: 'h

  • vue中路由跳转的方式有哪些你知道吗

    目录 第一种方式:router-link (声明式路由) 第二种方式:router.push(编程式路由) 第三种方式:this.$router.push() (函数里面调用) 第四种方式:this.$router.replace() (用法同上,push) 参考: 总结 第一种方式:router-link (声明式路由) 1. 不带参数 <router-link :to="{name:'home'}"> <router-link :to="{path:'/

随机推荐