Skip to content

Commit

Permalink
config/index.js添加配置浏览器标签显示的基本标题title的配置,会拼接为 '${title} - ${当前页标题}'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang.li committed Nov 19, 2018
1 parent e15918c commit 228cd68
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>iview-admin</title>
<title></title>
</head>
<body>
<noscript>
Expand Down
4 changes: 4 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default {
/**
* @description 配置显示在浏览器标签的title
*/
title: 'iView-admin',
/**
* @description token在Cookie中存储的天数,默认1天
*/
Expand Down
19 changes: 16 additions & 3 deletions src/libs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Cookies from 'js-cookie'
// cookie保存的天数
import config from '@/config'
import { forEach, hasOneOf, objEqual } from '@/libs/tools'
const { title, cookieExpires, useI18n } = config

export const TOKEN_KEY = 'token'

export const setToken = (token) => {
Cookies.set(TOKEN_KEY, token, {expires: config.cookieExpires || 1})
Cookies.set(TOKEN_KEY, token, {expires: cookieExpires || 1})
}

export const getToken = () => {
Expand Down Expand Up @@ -95,8 +96,8 @@ export const getRouteTitleHandled = (route) => {
export const showTitle = (item, vm) => {
let { title, __titleIsFunction__ } = item.meta
if (!title) return
if (vm.$config.useI18n) {
if (title.includes('{{') && title.includes('}}') && vm.$config.useI18n) title = title.replace(/({{[\s\S]+?}})/, (m, str) => str.replace(/{{([\s\S]*)}}/, (m, _) => vm.$t(_.trim())))
if (useI18n) {
if (title.includes('{{') && title.includes('}}') && useI18n) title = title.replace(/({{[\s\S]+?}})/, (m, str) => str.replace(/{{([\s\S]*)}}/, (m, _) => vm.$t(_.trim())))
else if (__titleIsFunction__) title = item.meta.title
else title = vm.$t(item.name)
} else title = (item.meta && item.meta.title) || item.name
Expand Down Expand Up @@ -384,3 +385,15 @@ export const scrollTop = (el, from = 0, to, duration = 500, endCallback) => {
}
scroll(from, to, step)
}

/**
* @description 根据当前跳转的路由设置显示在浏览器标签的title
* @param {Object} routeItem 路由对象
* @param {Object} vm Vue实例
*/
export const setTitle = (routeItem, vm) => {
const handledRoute = getRouteTitleHandled(routeItem)
const pageTitle = showTitle(handledRoute, vm)
const resTitle = pageTitle ? `${title} - ${pageTitle}` : title
window.document.title = resTitle
}
3 changes: 2 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Router from 'vue-router'
import routes from './routers'
import store from '@/store'
import iView from 'iview'
import { setToken, getToken, canTurnTo } from '@/libs/util'
import { setToken, getToken, canTurnTo, setTitle } from '@/libs/util'
import config from '@/config'
const { homeName } = config

Expand Down Expand Up @@ -53,6 +53,7 @@ router.beforeEach((to, from, next) => {
})

router.afterEach(to => {
setTitle(to, router.app)
iView.LoadingBar.finish()
window.scrollTo(0, 0)
})
Expand Down

0 comments on commit 228cd68

Please sign in to comment.