前言
本文主要介绍的是vue根据网站路由判断页面主题色的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
需求:
不同品牌对应不同版本配色
做法:
根据域名带的参数判断进入哪个品牌,对应哪个版本
在main.js中
import Vue from \'vue\'
import App from \'./App\'
import router from \'./router\'
import axios from \'axios\'
import MintUI from \'mint-ui\'
import { Indicator } from \'mint-ui\'
import { getUrls } from \'@/util/utils\'
import \'mint-ui/lib/style.css\'
import \'./css/index.css\'
Vue.use(MintUI)
//添加请求拦截器 loading
axios.interceptors.request.use(function (config) {
Indicator.open({
text: \'加载中...\',
spinnerType: \'fading-circle\'
})
return config
}),function (error) {
Indicator.close()
return Promise.reject(error)
}
axios.interceptors.response.use(function (config) {
Indicator.close()
return config
}),function (error) {
return Promise.reject(error)
}
Vue.prototype.$http = axios
Vue.prototype.getUrls = getUrls
router.beforeEach((to,from,next) => {
if (sessionStorage.getItem(\' color\')) {
document.documentElement.style.setProperty(\"--color\", sessionStorage.getItem(\' color\'))
next()
}
})
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: \'#app\',
router,
components: { App },
template: \'<App/>\'
})
在util.js中
export function getUrls() {
let colorValue
let url = window.location.href
let urlArr = url.split(\'?\')
let appU = urlArr[0].split(\'/\')
let styles = getComputedStyle(document.documentElement)
if (appU[appU.length-1] === \'login\') {
colorValue = styles.getPropertyValue(\'--OLAY\')
sessionStorage.setItem(\' color\', colorValue)
this.$router.push(\'/login\')
} else if (appU[appU.length-1] === \'resetPassword\') {
colorValue = styles.getPropertyValue(\'--pampers\')
sessionStorage.setItem(\' color\', colorValue)
this.$router.push(\'/login\')
}
}
在App.vue
<template>
<div id=\"app\">
<router-view/>
</div>
</template>
< >
export default {
name: \'App\',
created() {
this.getUrls()
}
}
</ >
<style>
:root {
--OLAY: rgb(237,202,138);
--pampers: rgb(5,183,185);
--color: #fff;
}
#app{
height: 100%;
}
</style>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
继续阅读与本文标签相同的文章
-
小程序如何引流?企业推广其实很简单
2026-05-19栏目: 教程
-
2019云栖大会 | 云原生时代 带你聚焦新数据库的硬核科技
2026-05-19栏目: 教程
-
AI人工智能了不得,帮助研究人员设计自行车,速度打破世界纪录
2026-05-19栏目: 教程
-
RNG官宣入驻快手,UZI成首席电竞官,S9成快手布局游戏圈跳板
2026-05-19栏目: 教程
-
国家邮政局官宣万国邮联国际小包终端费改革 你想知道的都在这里
2026-05-19栏目: 教程
