VUE實用的例子

表格中默認數(shù)據(jù)需要計算的時候先計算好再調(diào)用

//vue
<e-table
    :other-height="ComponentStateTable.otherHeight"
  />
//js
data() {
    // 計算高度值  
    const calculatedHeight = `${(window.innerHeight - 420) / 2}`;
    return {
      ComponentStateTable: {
        // 調(diào)用算好的高度
        otherHeight: calculatedHeight,
}

使用瀏覽器緩存來切換不同變量

場景:做了兩個主題來切換使用知纷,又不想保存在數(shù)據(jù)庫咏窿,那就用瀏覽器緩存來保存狀態(tài)禁谦。


image.png

網(wǎng)頁的設(shè)置文件main.js

//-----設(shè)置主題
// 從 localStorage 獲取 styleName 的值并設(shè)置為全局變量 window.styleName 的值
// 檢查localStorage中是否存在styleName  
const storedStyleName = localStorage.getItem('styleName');  
// 如果不存在率寡,則設(shè)置默認值theme1到window對象和localStorage中  
if (!storedStyleName) {  
  window.styleName = 'theme1';  
  localStorage.setItem('styleName', 'theme1');  
} else {  
  // 如果存在职烧,則直接從localStorage中獲取值賦給window對象  
  window.styleName = storedStyleName;  
}

//這里是判斷不同的主題變量使用不同的樣式
if (window.styleName === 'theme1') {
  import('@/themes/theme1/theme/index.css') 
  import('@/themes/theme1/mystyle.scss') 
  import('@/themes/theme1/variables.scss') 
  // alert(styleName)
} else if (window.styleName === 'theme2') {
  import('@/themes/theme2/theme/index.css') 
  import('@/themes/theme2/mystyle.scss') 
  import('@/themes/theme2/variables.scss') 
  // alert(styleName)
}

主題頁面.vue

<template>
  <div >
    <ul class="themeList">  
      <li v-for="(theme, index) in themes" :key="index" >  
        <div class="mystle">  
          <el-card class="box-card">  
            <div>  
              <p class="margin_b10 font_b">{{ theme.title }}</p>  
              <p><img :src="theme.image" style="width: 100%; height: auto;" /></p>  
              <p class="margin_t10 textcolor">{{ theme.industries }}</p>  
              <p class="margin_t10 textcolor">  
                <el-button  
                  v-if="currentStyleName === theme.name"  
                  type="success"  
                  @click="changeStyleName(theme.name)"  
                >  
                  <span>正在使用</span>  
                </el-button>  
                <el-button  
                  v-else  
                  type="primary"  
                  @click="changeStyleName(theme.name)"  
                >  
                  <span>立即使用</span>  
                </el-button>  
              </p>  
            </div>  
          </el-card>  
        </div>  
      </li>  
    </ul>  
  </div>

</template>
<script>
import Vue from 'vue';  

export default {
  data() {  
    return {  
      //獲取主題初始值
      currentStyleName: localStorage.getItem('styleName'), // 初始值
      //主題樣式
      themes: [  
      {  
        name: 'theme1',  
        title: '科技炫酷紅',  
        image: require('@/img/img_style1.jpg'),  
        industries: '油田虑绵,媒體,政治闽烙,預(yù)警' 
      },  
      {  
        name: 'theme2',  
        title: '大氣科技藍',  
        image: require('@/img/img_style2.jpg'),  
        industries: '科技捕发,電網(wǎng),醫(yī)療很魂,地鐵'
      },  
      // ...  
    ],  
    }
  },
  mounted() {
    // 從 localStorage 獲取 styleName 的值并設(shè)置為全局變量 window.styleName 的值
    window.styleName = localStorage.getItem('styleName') || 'theme1';
    
  },

  methods: {  
    changeStyleName(newStyle) {  
      // 更新window對象中的styleName屬性  
      window.styleName = newStyle;  
      // 更新localStorage中的styleName值  
      localStorage.setItem('styleName', newStyle); 
      console.log('localStorage.styleName set to:', newStyle);  
      // 刷新頁面  
      location.reload();
      // alert(window.styleName);
    }  
  
  }  
  
}
</script>

自定義tabs

// html
//控制顯示的按鈕 扎酷,其中按鈕的樣式自已優(yōu)化
<el-button :class="stationShow?'station_close':'station_open'"   @click="stationShow = true,stationShow2 = false"></el-button>
<el-button :class="stationShow2?'station_close':'station_open'"  @click="stationShow2 = true,stationShow = false"></el-button>
//顯示隱藏的容器
 <div v-show="stationShow" ></div>
<div v-show="stationShow2" ></div>


// JS
data() {
      return {
        stationShow:true,//定義一個要顯示的變量
    stationShow2:false,//定義第二個tab要顯示的變量
}
}

1、svg縮放拖拽組件

vue-svg-pan-zoom 組件

安裝組件:
方式1遏匆、執(zhí)行 npm install --save vue-svg-pan-zoom
方式2法挨、執(zhí)行 cnpm install vue-svg-pan-zoom

//頁面代碼

<template>
<SvgPanZoom
      class=""
      style="width: 100%;height:calc(100vh - 460px);"
      :zoomEnabled="true"
      :controlIconsEnabled="false"
      :fit="false"
      :center="true"
      :customEventsHandler="eventsHandler"
    >
 <svg  style="width: 100%;height: 180px" viewBox="0 0 707.8 195.12"></svg>
</SvgPanZoom>
</template>

<script>
//引用組件
  import SvgPanZoom from 'vue-svg-pan-zoom'
  export default {
    components: { SvgPanZoom }//控件聲明
  };
</script>

2、返回上一頁寫法

//html
<el-button type="primary" @click="goOff()"    >返回</el-button>

 //js 返回上一頁
methods: {
    goOff(){

        if (window.history.length <= 1) {

            this.$router.push({path:'/'})

            return false

        } else {

            this.$router.go(-1)

        }

    },
}

3幅聘、在JS中寫一個全局變量凡纳,其它界面調(diào)用

例如main.js中定義了一個styleName 的全局變量

window.styleName = 'theme2'//window為設(shè)置為全局變量

if (styleName === 'theme1') {
  import('@/themes/theme1/theme/index.css') 
  import('@/themes/theme1/mystyle.scss') 
  import('@/themes/theme1/variables.scss') 
  // alert(styleName)
} else if (styleName === 'theme2') {
  import('@/themes/theme2/theme/index.css') 
  import('@/themes/theme2/mystyle.scss') 
  import('@/themes/theme2/variables.scss') 
  // alert(styleName)
}

video.vue界面調(diào)用styleName變量

<template>
  <div class="">
    <video width="100%"   autoplay loop="loop" style="mix-blend-mode: lighten;">
      <source :src="`@/themes/${styleName}/img/map.mp4`"  type="video/mp4">
    </video>
  </div>
</template>
      
<script>

export default {  
  data() {  
    return {  
      styleName: ''  
    };  
  },  
  created() {  
    this.styleName = window.styleName;  
  }  
}
</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市喊暖,隨后出現(xiàn)的幾起案子惫企,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狞尔,死亡現(xiàn)場離奇詭異丛版,居然都是意外死亡,警方通過查閱死者的電腦和手機偏序,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門页畦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人研儒,你說我怎么就攤上這事豫缨。” “怎么了端朵?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵好芭,是天一觀的道長。 經(jīng)常有香客問我冲呢,道長舍败,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任敬拓,我火速辦了婚禮邻薯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘乘凸。我一直安慰自己厕诡,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布营勤。 她就那樣靜靜地躺著灵嫌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪冀偶。 梳的紋絲不亂的頭發(fā)上醒第,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機與錄音进鸠,去河邊找鬼。 笑死形病,一個胖子當(dāng)著我的面吹牛客年,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播漠吻,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼量瓜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了途乃?” 一聲冷哼從身側(cè)響起绍傲,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后烫饼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體猎塞,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年杠纵,在試婚紗的時候發(fā)現(xiàn)自己被綠了荠耽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡比藻,死狀恐怖铝量,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情银亲,我是刑警寧澤慢叨,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站务蝠,受9級特大地震影響插爹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜请梢,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一赠尾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧毅弧,春花似錦气嫁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至元咙,卻和暖如春梯影,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背庶香。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工甲棍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赶掖。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓感猛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親奢赂。 傳聞我的和親對象是個殘疾皇子陪白,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容