關(guān)于vue.js如何根據(jù)網(wǎng)站路由判斷頁(yè)面主題色教程詳解

2018-11-25 23:28:15 來(lái)源:互聯(lián)網(wǎng)作者:anne_zhou 人氣: 次閱讀 478 條評(píng)論

文章主要給大家介紹了關(guān)于vue如何根據(jù)網(wǎng)站路由判斷頁(yè)面主題色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,...

本文主要介紹的是vue根據(jù)網(wǎng)站路由判斷頁(yè)面主題色的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

需求:

不同品牌對(duì)應(yīng)不同版本配色

做法:

根據(jù)域名帶的參數(shù)判斷進(jìn)入哪個(gè)品牌,對(duì)應(yīng)哪個(gè)版本

在main.js中

  1. import Vue from 'vue'
  2. import App from './App'
  3. import router from './router'
  4. import axiOS from 'axios'
  5. import MintUI from 'mint-ui'
  6. import { Indicator } from 'mint-ui'
  7. import { getUrls } from '@/util/utils'
  8. import 'mint-ui/lib/style.css'
  9. import './css/index.css'
  10. Vue.use(MintUI)
  11. //添加請(qǐng)求攔截器 loading
  12. axios.interceptors.request.use(function (config) {
  13.  Indicator.open({
  14.  text: '加載中...',
  15.  spinnerType: 'fading-circle'
  16.  })
  17.  return config
  18. }),function (error) {
  19.  Indicator.close()
  20.  return Promise.reject(error)
  21. }
  22. axios.interceptors.response.use(function (config) {
  23.  Indicator.close()
  24.  return config
  25. }),function (error) {
  26.  return Promise.reject(error)
  27. }
  28.   
  29. Vue.prototype.$http = axios
  30. Vue.prototype.getUrls = getUrls
  31. router.beforeEach((to,from,next) => {
  32.  if (sessionStorage.getItem('basecolor')) {
  33.  document.documentElement.style.setProperty("--color", sessionStorage.getItem('basecolor'))
  34.  next()
  35.  }
  36. })
  37. Vue.config.productionTip = false
  38.   
  39. /* eslint-disable no-new */
  40. new Vue({
  41.  el: '#app',
  42.  router,
  43.  components: { App },
  44.  template: '<App/>'
  45. })

在util.js中

  1. export function getUrls() {
  2.  let colorValue
  3.  let url = window.location.href
  4.  let urlArr = url.split('?')
  5.  let appU = urlArr[0].split('/')
  6.  let styles = getComputedStyle(document.documentElement)
  7.  if (appU[appU.length-1] === 'login') {
  8.  colorValue = styles.getPropertyValue('--OLAY')
  9.  sessionStorage.setItem('basecolor', colorValue)
  10.  this.$router.push('/login')
  11.  } else if (appU[appU.length-1] === 'resetPassword') {
  12.  colorValue = styles.getPropertyValue('--pampers')
  13.  sessionStorage.setItem('basecolor', colorValue)
  14.  this.$router.push('/login')
  15.  }
  16. }

在App.vue

  1. <template>
  2.  <div id="app">
  3.  <router-view/>
  4.  </div>
  5. </template>
  6.   
  7. <script>
  8.  export default {
  9.  name: 'App',
  10.  created() {
  11.   this.getUrls()
  12.  }
  13. }
  14. </script>
  15.   
  16. <style>
  17.  :root {
  18.  --OLAY: rgb(237,202,138);
  19.  --pampers: rgb(5,183,185);
  20.  --color: #fff;
  21.  }
  22.  #app{
  23.  height: 100%;
  24.  }
  25. </style>

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。

    無(wú)相關(guān)信息