1. 设置标题,兼容iOS和安卓
setBrowserTitle: (title, img) => {
if (title === undefined || window.document.title === title) {
return
}
document.title = title
const userAgent = navigator.userAgent.toLowerCase()
if (/iphone|ipad|ipod/.test(userAgent)) {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
iframe.setAttribute('src', img || '/favicon.ico')
const iframeCallback = function() {
setTimeout(function() {
iframe.removeEventListener('load', iframeCallback)
document.body.removeChild(iframe)
}, 0)
}
iframe.addEventListener('load', iframeCallback)
document.body.appendChild(iframe)
}
},
2. vue项目国际化处理
import lang from '@/i18n'
const i18n = {
setTitle: (name, title) => {
title = i18n.generateTitle(name, title)
i18n.setBrowserTitle((title ? title + '-' : '') + i18n.t('platform') + '-' + i18n.t('company'))
},
setBrowserTitle: (title, img) => {
if (title === undefined || window.document.title === title) {
return
}
document.title = title
const userAgent = navigator.userAgent.toLowerCase()
if (/iphone|ipad|ipod/.test(userAgent)) {
const iframe = document.createElement('iframe')
iframe.style.display = 'none'
iframe.setAttribute('src', img || '/favicon.ico')
const iframeCallback = function() {
setTimeout(function() {
iframe.removeEventListener('load', iframeCallback)
document.body.removeChild(iframe)
}, 0)
}
iframe.addEventListener('load', iframeCallback)
document.body.appendChild(iframe)
}
},
generateTitle: (name, title, ...values) => {
if (!name && !title) return
const hasKey = lang.te('route.' + name)
if (hasKey) {
return lang.t('route.' + name, ...values)
}
if (title) { return title }
name = name.substring(name.lastIndexOf('.') + 1, name.length)
return name
},
t: (path, ...values) => {
const hasKey = lang.te(path)
if (hasKey) {
return lang.t(path, ...values)
}
path = path.substring(path.lastIndexOf('.') + 1, path.length)
return path
},
te: (path) => {
return lang.te(path)
}
}
export default i18n