vue-router動(dòng)態(tài)設(shè)置頁(yè)面title的實(shí)例講解
由于用Vue框架開發(fā)的應(yīng)用是SPA(單頁(yè)面應(yīng)用),采用的是路由的形式,沒有所謂的頁(yè)面,所以想讓網(wǎng)頁(yè)的標(biāo)題隨著路由的改變而改變,可以使用document.title = ×××來改變網(wǎng)頁(yè)標(biāo)題。
但是在IOS APP里這種方式不起作用,原因是在IOS webview中網(wǎng)頁(yè)標(biāo)題只加載一次,動(dòng)態(tài)改變是無效的。
解決方案是在路由切換完成之后,靜默加載一個(gè)空的iframe動(dòng)態(tài)設(shè)置title
util.js中定義setMetaTitle()函數(shù)
function setMetaTitle(title) {
document.title = title
let mobile = navigator.userAgent.toLowerCase()
if (/iphone|ipad|ipod/.test(mobile)) {
let iframe = document.createElement('iframe')
iframe.style.display = 'none'
// 替換成站標(biāo)favicon路徑或者任意存在的較小的圖片即可
iframe.setAttribute('src', 'static/img/blank.png')
let iframeCallback = function () {
setTimeout(function () {
iframe.removeEventListener('load', iframeCallback)
document.body.removeChild(iframe)
}, 0)
}
iframe.addEventListener('load', iframeCallback)
document.body.appendChild(iframe)
}
}
export {
setMetaTitle
}
應(yīng)用入口main.js中調(diào)用setMetaTitle()函數(shù)
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import routes from './router/router.js'
import filter from './utils/filter.js'
import { setMetaTitle } from './utils/util.js'
import App from './App.vue'
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.directive('title', {
inserted: function (el, binding) {
setMetaTitle(binding.value)
}
})
Object.keys(filter).forEach(function(k) {
Vue.filter(k, filter[k]);
});
const router = new VueRouter({
routes: routes
})
new Vue({
router: router,
render: h => h(App)
}).$mount('#app')
vue文件調(diào)用
<h2 v-title="'頁(yè)面標(biāo)題'">通過指令設(shè)置頁(yè)面標(biāo)題</h2>
以上這篇vue-router動(dòng)態(tài)設(shè)置頁(yè)面title的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Vue render函數(shù)在ElementUi中的應(yīng)用
今天小編就為大家分享一篇淺談Vue render函數(shù)在ElementUi中的應(yīng)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
詳解vue-property-decorator使用手冊(cè)
這篇文章主要介紹了vue-property-decorator使用手冊(cè),文中較詳細(xì)的給大家介紹了他們的用法,通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vite多頁(yè)面配置項(xiàng)目實(shí)戰(zhàn)
vite官方文檔中有關(guān)于多頁(yè)面應(yīng)用模式如果配置的說明,下面這篇文章主要給大家介紹了關(guān)于vite多頁(yè)面配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
vue實(shí)現(xiàn)全選組件封裝實(shí)例詳解
這篇文章主要介紹了vue?全選組件封裝,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
vue3+ts+vant移動(dòng)端H5項(xiàng)目搭建的實(shí)現(xiàn)步驟
本文主要介紹了vue3+ts+vant移動(dòng)端H5項(xiàng)目搭建,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

