Skip to content

Commit 228cd68

Browse files
author
zhigang.li
committed
config/index.js添加配置浏览器标签显示的基本标题title的配置,会拼接为 '${title} - ${当前页标题}'
1 parent e15918c commit 228cd68

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width,initial-scale=1.0">
77
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
8-
<title>iview-admin</title>
8+
<title></title>
99
</head>
1010
<body>
1111
<noscript>

src/config/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export default {
2+
/**
3+
* @description 配置显示在浏览器标签的title
4+
*/
5+
title: 'iView-admin',
26
/**
37
* @description token在Cookie中存储的天数,默认1天
48
*/

src/libs/util.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import Cookies from 'js-cookie'
22
// cookie保存的天数
33
import config from '@/config'
44
import { forEach, hasOneOf, objEqual } from '@/libs/tools'
5+
const { title, cookieExpires, useI18n } = config
56

67
export const TOKEN_KEY = 'token'
78

89
export const setToken = (token) => {
9-
Cookies.set(TOKEN_KEY, token, {expires: config.cookieExpires || 1})
10+
Cookies.set(TOKEN_KEY, token, {expires: cookieExpires || 1})
1011
}
1112

1213
export const getToken = () => {
@@ -95,8 +96,8 @@ export const getRouteTitleHandled = (route) => {
9596
export const showTitle = (item, vm) => {
9697
let { title, __titleIsFunction__ } = item.meta
9798
if (!title) return
98-
if (vm.$config.useI18n) {
99-
if (title.includes('{{') && title.includes('}}') && vm.$config.useI18n) title = title.replace(/({{[\s\S]+?}})/, (m, str) => str.replace(/{{([\s\S]*)}}/, (m, _) => vm.$t(_.trim())))
99+
if (useI18n) {
100+
if (title.includes('{{') && title.includes('}}') && useI18n) title = title.replace(/({{[\s\S]+?}})/, (m, str) => str.replace(/{{([\s\S]*)}}/, (m, _) => vm.$t(_.trim())))
100101
else if (__titleIsFunction__) title = item.meta.title
101102
else title = vm.$t(item.name)
102103
} else title = (item.meta && item.meta.title) || item.name
@@ -384,3 +385,15 @@ export const scrollTop = (el, from = 0, to, duration = 500, endCallback) => {
384385
}
385386
scroll(from, to, step)
386387
}
388+
389+
/**
390+
* @description 根据当前跳转的路由设置显示在浏览器标签的title
391+
* @param {Object} routeItem 路由对象
392+
* @param {Object} vm Vue实例
393+
*/
394+
export const setTitle = (routeItem, vm) => {
395+
const handledRoute = getRouteTitleHandled(routeItem)
396+
const pageTitle = showTitle(handledRoute, vm)
397+
const resTitle = pageTitle ? `${title} - ${pageTitle}` : title
398+
window.document.title = resTitle
399+
}

src/router/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Router from 'vue-router'
33
import routes from './routers'
44
import store from '@/store'
55
import iView from 'iview'
6-
import { setToken, getToken, canTurnTo } from '@/libs/util'
6+
import { setToken, getToken, canTurnTo, setTitle } from '@/libs/util'
77
import config from '@/config'
88
const { homeName } = config
99

@@ -53,6 +53,7 @@ router.beforeEach((to, from, next) => {
5353
})
5454

5555
router.afterEach(to => {
56+
setTitle(to, router.app)
5657
iView.LoadingBar.finish()
5758
window.scrollTo(0, 0)
5859
})

0 commit comments

Comments
 (0)