赞
踩
https://element.eleme.cn/#/zh-CN/theme
➜ Genes-Admin git:(ogenes) sudo n 10.16.0
➜ Genes-Admin git:(ogenes) npm -v
6.9.0
➜ Genes-Admin git:(ogenes) node -v
v10.16.0
➜ Genes-Admin git:(ogenes) sudo npm i element-theme -g --unsafe-perm=true --allow-root
➜ Genes-Admin git:(ogenes) ✗ et -i element-variables-light.scss
➜ Genes-Admin git:(ogenes) ✗ et -i element-variables-dark.scss
#light ➜ Genes-Admin git:(ogenes) ✗ mkdir -p resources/src/assets/theme/dark resources/src/assets/theme/light ➜ Genes-Admin git:(ogenes) ✗ et -c element-variables-light.scss ✔ build element theme ✔ build theme font ➜ Genes-Admin git:(ogenes) ✗ mv theme/fonts resources/src/assets/theme/light ➜ Genes-Admin git:(ogenes) ✗ mv theme/index.css resources/src/assets/theme/light ➜ Genes-Admin git:(ogenes) ✗ rm -rf theme #dark ➜ Genes-Admin git:(ogenes) ✗ et -c element-variables-dark.scss ✔ build element theme ✔ build theme font ➜ Genes-Admin git:(ogenes) ✗ mv theme/fonts resources/src/assets/theme/dark ➜ Genes-Admin git:(ogenes) ✗ mv theme/index.css resources/src/assets/theme/dark ➜ Genes-Admin git:(ogenes) ✗ rm -rf theme
#vim resources/src/main.js
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
#加入自定义主题样式
import '@/assets/theme/dark/index.css'
import '@/styles/index.scss' // global css
#vim resources/src/settings.js ,加上以下配置
/**
* @type {string} light | dark
* @description theme
*/
theme: 'dark'
import defaultSettings from '@/settings' const { showSettings, fixedHeader, sidebarLogo, theme } = defaultSettings function getItem(key, def) { return localStorage.getItem(key) === null ? def : localStorage.getItem(key); } function setItem(key, value) { localStorage.setItem(key, value); } const state = { showSettings: getItem('showSettings', showSettings), fixedHeader: getItem('fixedHeader', fixedHeader), sidebarLogo: getItem('sidebarLogo', sidebarLogo), theme: getItem('theme', theme), } const mutations = { CHANGE_SETTING: (state, { key, value }) => { // eslint-disable-next-line no-prototype-builtins if (state.hasOwnProperty(key)) { state[key] = value setItem(key, value); } } } const actions = { changeSetting({ commit }, data) { commit('CHANGE_SETTING', data) } } export default { namespaced: true, state, mutations, actions }
#新建文件 resources/src/styles/index.js
import store from '@/store'
if (store.state.settings.theme === 'dark') {
import('@/assets/theme/dark/index.css');
} else {
import('@/assets/theme/light/index.css');
}
import '@/styles/index.scss' // global css
#修改main.js
// import '@/styles/index.scss' // global css
import '@/styles' // global css
测试
#临时测试,可以模拟语言切换的下拉框 #vim resources/src/components/Theme/index.vue <template> <el-dropdown @command="handleCommand"> <span class="el-dropdown-link"> {{ theme }} </span> <el-dropdown-menu slot="dropdown"> <el-dropdown-item v-for="(val, key) in themes" :disabled="val===theme" :key="key" :command="key"> {{ val }} </el-dropdown-item> </el-dropdown-menu> </el-dropdown> </template> <script> export default { name: "Theme", data() { return { themes: { light: '白天模式', dark: '黑夜模式', }, } }, computed: { theme() { return this.themes[this.$store.state.settings.theme]; } }, methods: { handleCommand(val) { this.$store.dispatch('settings/changeSetting', { key: 'theme', value: val }) this.$message.success('switch theme success') location.reload() } } } </script> <style scoped> </style> #然后放到 resources/src/layout/components/Navbar.vue <theme id="header-theme" class="right-menu-item"/> import Theme from "@/components/Theme"; export default { components: { Breadcrumb, Hamburger, Languages, Theme }, …… }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。