语言功能
后端从url获取lang参数决定返回的语言 在utils中创建language.js提供方法
import Cookies from 'js-cookie'
const languageKey = 'admin-language'
export function getLanguage() {
return Cookies.get(languageKey)
}
export function setLanguage(language) {
return Cookies.set(languageKey, language)
}
然后在请求拦截器中对所有请求设置全局参数
service.interceptors.request.use(
config => {
if (store.getters.token) {
config.headers['Authorization'] = getToken()
config.params = { 'lang': getLanguage() }
}
return config
},
error => {
console.log(error)
return Promise.reject(error)
}
)
补充:
登录部分
const actions = {
login({ commit }, userInfo) {
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ email: username.trim(), password: password.trim() }).then(response => {
const { data } = response
commit('SET_TOKEN', data.token)
setToken(data.token)
setRefreshToken(data.refreshToken)
resolve()
}).catch(error => {
reject(error)
})
})
}
commit指向的 相关连接
const mutations = {
RESET_STATE: (state) => {
Object.assign(state, getDefaultState())
},
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
}
}
路由设置
单级页面路由
{
path: '/user',
component: Layout,
children: [
{
path: 'index',
name: 'userManager',
component: () => import('@/views/form/index'),
meta: { title: 'Form', icon: 'form' }
}
]
},
二级路由
{
path: '/course',
component: Layout,
redirect: '/course',
name: 'courseManager',
meta: { title: '课程管理', icon: 'el-icon-s-help' },
children: [
{
path: 'table',
name: 'Table',
component: () => import('@/views/table/index'),
meta: { title: 'Table', icon: 'table' }
},
{
path: 'tree',
name: 'Tree',
component: () => import('@/views/tree/index'),
meta: { title: 'Tree', icon: 'tree' }
}
]
}
三级路由
{
path: '/nested',
component: Layout,
redirect: '/nested/menu1',
name: 'Nested',
meta: {
title: 'Nested',
icon: 'nested'
},
children: [
{
path: 'menu1',
component: () => import('@/views/nested/menu1/index'),
name: 'Menu1',
meta: { title: 'Menu1' },
children: [
{
path: 'menu1-1',
component: () => import('@/views/nested/menu1/menu1-1'),
name: 'Menu1-1',
meta: { title: 'Menu1-1' }
},
{
path: 'menu1-2',
component: () => import('@/views/nested/menu1/menu1-2'),
name: 'Menu1-2',
meta: { title: 'Menu1-2' },
children: [
{
path: 'menu1-2-1',
component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
name: 'Menu1-2-1',
meta: { title: 'Menu1-2-1' }
},
{
path: 'menu1-2-2',
component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
name: 'Menu1-2-2',
meta: { title: 'Menu1-2-2' }
}
]
},
{
path: 'menu1-3',
component: () => import('@/views/nested/menu1/menu1-3'),
name: 'Menu1-3',
meta: { title: 'Menu1-3' }
}
]
},
{
path: 'menu2',
component: () => import('@/views/nested/menu2/index'),
name: 'Menu2',
meta: { title: 'menu2' }
}
]
}
|