vue動態(tài)設置頁面title的方法實例
本文主要介紹了Vue動態(tài)修改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'//動態(tài)修改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 導航守衛(wèi)來修改
//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的鉤子函數(shù),在進入路由前執(zhí)行
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動態(tài)設置頁面title的文章就介紹到這了,更多相關vue動態(tài)設置頁面title內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Vue中導入并讀取Excel數(shù)據(jù)的操作步驟
在工作中遇到需要前端上傳excel文件獲取到相應數(shù)據(jù)處理之后傳給后端并且展示上傳文件的數(shù)據(jù),所以本文就來給大家介紹一下Vue中導入并讀取Excel數(shù)據(jù)的操作步驟,需要的朋友可以參考下2023-08-08
vue大文件分片上傳之simple-uploader.js的使用
本文主要介紹了vue大文件分片上傳之simple-uploader.js的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

