您好,登錄后才能下訂單哦!
這篇文章主要介紹了vue實(shí)現(xiàn)根據(jù)網(wǎng)站路由判斷頁面主題色的示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
需求:
不同品牌對應(yīng)不同版本配色
做法:
根據(jù)域名帶的參數(shù)判斷進(jìn)入哪個(gè)品牌,對應(yīng)哪個(gè)版本
在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('basecolor')) { document.documentElement.style.setProperty("--color", sessionStorage.getItem('basecolor')) 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('basecolor', colorValue) this.$router.push('/login') } else if (appU[appU.length-1] === 'resetPassword') { colorValue = styles.getPropertyValue('--pampers') sessionStorage.setItem('basecolor', colorValue) this.$router.push('/login') } }
在App.vue
<template> <div id="app"> <router-view/> </div> </template> <script> export default { name: 'App', created() { this.getUrls() } } </script> <style> :root { --OLAY: rgb(237,202,138); --pampers: rgb(5,183,185); --color: #fff; } #app{ height: 100%; } </style>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“vue實(shí)現(xiàn)根據(jù)網(wǎng)站路由判斷頁面主題色的示例”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。