有关Vue中如何换肤?
本篇文章主要介绍了Vue 换肤的示例实践,现在分享给大家,也给大家做个参考。
最近公司做的项目得到一个网站换肤的需求,也就是切换主题。那么如何切换主题色呢?切换主题色其实就是切换 CSS,然而在项目中不仅只有 CSS 需要换肤,图标和图片也需要跟随主题进行切换。于是,写一篇文章来记录下 Vue 中实现换肤的过程,先看下效果吧。
本文主要分三部分:CSS 切换,图标切换和图片切换。
CSS切换
关于 CSS 颜色的切换,我通过搜索,参考了ElementUI 的方案,总的来说分为四步
在 static 目录下新建一个 theme.css 文件,将需要替换的 CSS 声明在此文件中
.side-bar { background: linear-gradient(#B7A3FF, #879FFF) !important; } .side-bar .account-info { background: #8981D8 !important; }
声明所有可选的主题,每种颜色都对应于一个关键词,方便区分
colors: [{ themeId: 1, familyPrimary: '#B7A3FF', familySecondary: '#879FFF', sideBarTop: '#8981D8' }, { themeId: 2, familyPrimary: '#FDC5C5', familySecondary: '#F070A0', sideBarTop: '#E7829F' }, { themeId: 3, familyPrimary: '#414D6C', familySecondary: '#2D1E3C', sideBarTop: '#423C50' }]
通过 AJAX 获取 theme.css ,将颜色值替换为关键词。
getFile(`/static/theme.css`) .then(({data}) => { let style = getStyleTemplate(data) }) function getStyleTemplate (data) { const colorMap = { '#B7A3FF': 'familyPrimary', '#879FFF': 'familySecondary', '#8981D8': 'sideBarTop' } Object.keys(colorMap).forEach(key => { const value = colorMap[key] data = data.replace(new RegExp(key, 'ig'), value) }) return data }
把关键词再换回刚刚生成的相应的颜色值,并在页面上添加 style 标签
getFile(`/static/theme.css`) .then(({data}) => { let style = getStyleTemplate(data) writeNewStyle(style, this.color) }) function writeNewStyle (originalStyle, colors) { let oldEl = document.getElementById('temp-style') let cssText = originalStyle Object.keys(colors).forEach(key => { cssText = cssText.replace(new RegExp(key, 'ig'), colors[key]) }) const style = document.createElement('style') style.innerText = cssText style.id = 'temp-style' oldEl ? document.head.replaceChild(style, oldEl) : document.head.appendChild(style) }
图标切换
由于项目刚开始做的时候并没有考虑到换肤的需求,于是所有图标都是采用 img 标签的方式引用的,
<img src="../../assets/icon_edit.svg">
这样就导致无法给 icon 动态切换颜色了,所以,我决定改为 font 文件的方式来使用图标。这里推荐一个网站 icomoon ,这个网站可以轻松地将图片转换成 font 文件。图标也非常适合通过 font 的方式来使用,我们可以更加方便的修改图标的大小和颜色。
通过在线转换,我们将下载下来的 font 文件放入项目中,并新建一个 CSS 文件来声明所有图标。
@font-face { font-family: 'icomoon'; src: url('../assets/fonts/icomoon.eot?vpkwno'); src: url('../assets/fonts/icomoon.eot?vpkwno#iefix') format('embedded-opentype'), url('../assets/fonts/icomoon.ttf?vpkwno') format('truetype'), url('../assets/fonts/icomoon.woff?vpkwno') format('woff'), url('../assets/fonts/icomoon.svg?vpkwno#icomoon') format('svg'); font-weight: normal; font-style: normal; } [class^="icon-"], [class*=" icon-"] { /* use !important to prevent issues with browser extensions that change fonts */ font-family: 'icomoon' !important; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; line-height: 1; vertical-align: sub; /* Better Font Rendering =========== */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .icon-edit:before { content: "\e900"; }
之后就能通过 CSS 类名的方式来引用图标了。
<span class="icon-edit"></span>
为了使主题生效,我们也需要把图标的 CSS 写入 theme.css 文件中
.icon_edit:before { background-image: linear-gradient(-135deg, #879FFF 0%, #B7A3FF 100%); }
图片切换
项目中还存在很多占位图或者其他图片会随着主题的变化而变化。通过引入所有图片,并用文件名来区分不同主题所对应的图片。在点击切换主题时,切换到主题所对应的文件,就能实现图片切换了。为此,我写了一个 mixin,并在组件中引入 mixin。
<img :src="userImg || placeholderWoman">
placeholderMixin
let callback const placeholderMixin = { data () { return { placeholderWoman: '', placeHolderNoReply: '', placeHolderNothing: '' } }, created () { let themeId = localStorage.getItem('themeId') let theme = themeId2Name(themeId) this.setThemeValue(theme) callback = (theme) => { this.setThemeValue(theme) } bus.$on('changeTheme', callback) }, destroyed () { bus.$off('changeTheme', callback) }, methods: { setThemeValue (theme) { this.placeholderWoman = require(`@/assets/placeholder_woman_${theme}.svg`) this.placeHolderNoReply = require(`@/assets/icon_noreply_${theme}.svg`) this.placeHolderNothing = require(`@/assets/icon_nothing_${theme}.svg`) } } }
在点击切换主题时,会发射一个 changeTheme 事件,各组件接收到 changeTheme 事件,就会为图片重新赋值,也就达到了切换图片的效果。
let theme = themeId2Name(this.themeId) bus.$emit('changeTheme', theme)
这样也就达到了切换主题的效果,但是这种方法需要在几乎所有业务组件中引入 mixin,如果有更好的方法,欢迎与我交流。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
以上是有关Vue中如何换肤?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可以通过以下步骤为 Vue 按钮添加函数:将 HTML 模板中的按钮绑定到一个方法。在 Vue 实例中定义该方法并编写函数逻辑。

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

在 Vue.js 中引用 JS 文件的方法有三种:直接使用 <script> 标签指定路径;利用 mounted() 生命周期钩子动态导入;通过 Vuex 状态管理库进行导入。

Vue.js 中的 watch 选项允许开发者监听特定数据的变化。当数据发生变化时,watch 会触发一个回调函数,用于执行更新视图或其他任务。其配置选项包括 immediate,用于指定是否立即执行回调,以及 deep,用于指定是否递归监听对象或数组的更改。

Vue.js 返回上一页有四种方法:$router.go(-1)$router.back()使用 <router-link to="/"> 组件window.history.back(),方法选择取决于场景。

可以通过以下方法查询 Vue 版本:使用 Vue Devtools 在浏览器的控制台中查看“Vue”选项卡。使用 npm 运行“npm list -g vue”命令。在 package.json 文件的“dependencies”对象中查找 Vue 项。对于 Vue CLI 项目,运行“vue --version”命令。检查 HTML 文件中引用 Vue 文件的 <script> 标签中的版本信息。

Vue 多页面开发是一种使用 Vue.js 框架构建应用程序的方法,其中应用程序被划分为独立的页面:代码维护性:将应用程序拆分为多个页面可以使代码更易于管理和维护。模块化:每个页面都可以作为独立的模块,便于重用和替换。路由简单:页面之间的导航可以通过简单的路由配置来管理。SEO 优化:每个页面都有自己的 URL,这有助于搜索引擎优化。

Vue 中 div 元素跳转的方法有两种:使用 Vue Router,添加 router-link 组件。添加 @click 事件监听器,调用 this.$router.push() 方法跳转。
