vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
想要讓瀏覽器的標(biāo)題,隨著vue的路由跳轉(zhuǎn)的改變而改變,就要配置router/index.js文件里的信息。在meta對(duì)象里面配置一個(gè)title。
{ ? ? path: "/", ? ? name: "Home", ? ? meta: { ? ? ? title: "首頁"http://這是重點(diǎn) ? ? }, ? ? component: () => import( /* webpackChunkName: "about" */ "../views/home/index.vue"), ? }
然后在路由跳轉(zhuǎn)之前判斷跳轉(zhuǎn)之后的頁面是否配置的有title值。如果有則替換title,如果沒有則保持title不變即可。
router.beforeEach((to, from, next) => { ?? ? if (to.meta.title) { //如果設(shè)置標(biāo)題,攔截后設(shè)置標(biāo)題 ? ? document.title = to.meta.title ? } ? })
配置成功之后,vue在進(jìn)行路由跳轉(zhuǎn)的時(shí)候,頁面的title也會(huì)跟著路由配置的信息進(jìn)行跳轉(zhuǎn)。
--------2022-06-14補(bǔ)充--------
第二種方式:使用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="暫無數(shù)據(jù)" 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動(dòng)態(tài)改變title
需求
1.不同路由路徑下,動(dòng)態(tài)更改title
2.相同路徑下,像產(chǎn)品詳情頁,需要根據(jù)產(chǎn)品名字不同動(dòng)態(tài)更改title
解決需求一
1.在router.js根據(jù)不同的路由配置所屬title
{ ? ? path: '/startCertificate', ? ? name: 'startCertificate', ? ? component: startCertificate, ? ? meta:{ ? ? ? title:'這是動(dòng)態(tài)路由哦' ? ? } ?},
2.在main.js中配置
情況一:普通h5開發(fā)
router.beforeEach((to,from,next) =>{ ? ? // 路由發(fā)生變化修改頁面title ? ?if (to.meta.title) { ? ? ?document.title = to.meta.title; ? ?} }
情況二:在app內(nèi)嵌h5的混合應(yīng)用中,iOS系統(tǒng)下部分APP的webview中的標(biāo)題不能通過 document.title = xxx 的方式修改,因?yàn)樵贗OS webview中網(wǎng)頁標(biāo)題只加載一次,動(dòng)態(tài)改變是無效的,解決代碼如下
router.afterEach(route => { ? // 從路由的元信息中獲取 title 屬性 ? if (route.meta.title) { ? ? document.title = route.meta.title; ? ? // 如果是 iOS 設(shè)備,則使用如下 hack 的寫法實(shí)現(xiàn)頁面標(biāo)題的更新 ? ? 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 = '動(dòng)態(tài)title' ? ? }, } </script>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue通過數(shù)據(jù)過濾實(shí)現(xiàn)表格合并
這篇文章主要為大家詳細(xì)介紹了vue通過數(shù)據(jù)過濾實(shí)現(xiàn)表格合并,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用)
這篇文章主要介紹了多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06VUE實(shí)現(xiàn)強(qiáng)制渲染,強(qiáng)制更新
今天小編就為大家分享一篇VUE實(shí)現(xiàn)強(qiáng)制渲染,強(qiáng)制更新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10vue 使用Jade模板寫html,stylus寫css的方法
這篇文章主要介紹了vue 使用Jade模板寫html,stylus寫css的方法,文中還給大家提到了使用jade注意事項(xiàng),需要的朋友可以參考下2018-02-02Element-ui樹形控件el-tree自定義增刪改和局部刷新及懶加載操作
這篇文章主要介紹了Element-ui樹形控件el-tree自定義增刪改和局部刷新及懶加載操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08