vue中elementUI里面一些插件的使用
全屏插件的引用
全屏功能可以使用插件來(lái)實(shí)現(xiàn)
第一步,安裝全局插件screenfull
npm i screenfull@5.1.0
第二步,封裝全屏顯示的插件 ScreenFull/index.vue
<template> <div> <!-- 放置一個(gè)按鈕 --> <el-button class="icon" @click="changeScreen">全屏</el-button> </div> </template> <script> import ScreenFull from 'screenfull' export default { methods: { // 改變?nèi)? changeScreen() { if (!ScreenFull.isEnabled) { // 此時(shí)全屏不可用 this.$message.warning('此時(shí)全屏組件不可用') return } // document.documentElement.requestFullscreen() 原生js調(diào)用 // 如果可用 就可以全屏 ScreenFull.toggle() } } } </script> <style scoped> .icon{ width: 40px; height: 40px; background-color: #ff0; } </style>
第三步,全局注冊(cè)該組件 main.js
import ScreenFull from './ScreenFull' Vue.component('ScreenFull', ScreenFull) // 注冊(cè)全屏組件
第四步,放置于 App.vue 中
<template> <div id="app"> //全屏按鈕 <screen-full /> </div> </template> <script> export default { name: 'App' } </script>
動(dòng)態(tài)主題的設(shè)置
我們想要實(shí)現(xiàn)在頁(yè)面中實(shí)時(shí)的切換顏色,此時(shí)頁(yè)面的主題可以跟著設(shè)置的顏色進(jìn)行變化
簡(jiǎn)單說(shuō)明一下它的原理: element-ui 2.0 版本之后所有的樣式都是基于 SCSS 編寫的,所有的顏色都是基于幾個(gè)基礎(chǔ)顏色變量來(lái)設(shè)置的,所以就不難實(shí)現(xiàn)動(dòng)態(tài)換膚了,只要找到那幾個(gè)顏色變量修改它就可以了。 首先我們需要拿到通過(guò) package.json
拿到 element-ui 的版本號(hào),根據(jù)該版本號(hào)去請(qǐng)求相應(yīng)的樣式。拿到樣式之后將樣色,通過(guò)正則匹配和替換,將顏色變量替換成你需要的,之后動(dòng)態(tài)添加 style
標(biāo)簽來(lái)覆蓋原有的 css 樣式。
第一步, 封裝顏色選擇組件 ThemePicker
代碼地址:@/components/ThemePicker。
實(shí)現(xiàn)代碼
<template> <el-color-picker v-model="theme" :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]" class="theme-picker" popper-class="theme-picker-dropdown" /> </template> <script> const version = require('element-ui/package.json').version // element-ui version from node_modules const ORIGINAL_THEME = '#409EFF' // default color export default { data() { return { chalk: '', // content of theme-chalk css theme: '' } }, computed: { defaultTheme() { return this.$store.state.settings.theme } }, watch: { defaultTheme: { handler: function(val, oldVal) { this.theme = val }, immediate: true }, async theme(val) { const oldVal = this.chalk ? this.theme : ORIGINAL_THEME if (typeof val !== 'string') return const themeCluster = this.getThemeCluster(val.replace('#', '')) const originalCluster = this.getThemeCluster(oldVal.replace('#', '')) console.log(themeCluster, originalCluster) const $message = this.$message({ message: ' Compiling the theme', customClass: 'theme-message', type: 'success', duration: 0, iconClass: 'el-icon-loading' }) const getHandler = (variable, id) => { return () => { const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', '')) const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster) let styleTag = document.getElementById(id) if (!styleTag) { styleTag = document.createElement('style') styleTag.setAttribute('id', id) document.head.appendChild(styleTag) } styleTag.innerText = newStyle } } if (!this.chalk) { const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css` await this.getCSSString(url, 'chalk') } const chalkHandler = getHandler('chalk', 'chalk-style') chalkHandler() const styles = [].slice.call(document.querySelectorAll('style')) .filter(style => { const text = style.innerText return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text) }) styles.forEach(style => { const { innerText } = style if (typeof innerText !== 'string') return style.innerText = this.updateStyle(innerText, originalCluster, themeCluster) }) this.$emit('change', val) $message.close() } }, methods: { updateStyle(style, oldCluster, newCluster) { let newStyle = style oldCluster.forEach((color, index) => { newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index]) }) return newStyle }, getCSSString(url, variable) { return new Promise(resolve => { const xhr = new XMLHttpRequest() xhr.onreadystatechange = () => { if (xhr.readyState === 4 && xhr.status === 200) { this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '') resolve() } } xhr.open('GET', url) xhr.send() }) }, getThemeCluster(theme) { const tintColor = (color, tint) => { let red = parseInt(color.slice(0, 2), 16) let green = parseInt(color.slice(2, 4), 16) let blue = parseInt(color.slice(4, 6), 16) if (tint === 0) { // when primary color is in its rgb space return [red, green, blue].join(',') } else { red += Math.round(tint * (255 - red)) green += Math.round(tint * (255 - green)) blue += Math.round(tint * (255 - blue)) red = red.toString(16) green = green.toString(16) blue = blue.toString(16) return `#${red}${green}${blue}` } } const shadeColor = (color, shade) => { let red = parseInt(color.slice(0, 2), 16) let green = parseInt(color.slice(2, 4), 16) let blue = parseInt(color.slice(4, 6), 16) red = Math.round((1 - shade) * red) green = Math.round((1 - shade) * green) blue = Math.round((1 - shade) * blue) red = red.toString(16) green = green.toString(16) blue = blue.toString(16) return `#${red}${green}${blue}` } const clusters = [theme] for (let i = 0; i <= 9; i++) { clusters.push(tintColor(theme, Number((i / 10).toFixed(2)))) } clusters.push(shadeColor(theme, 0.1)) return clusters } } } </script> <style> .theme-message, .theme-picker-dropdown { z-index: 99999 !important; } .theme-picker .el-color-picker__trigger { height: 26px !important; width: 26px !important; padding: 2px; } .theme-picker-dropdown .el-color-dropdown__link-btn { display: none; } .el-color-picker { height: auto !important; } </style>
注冊(cè)代碼
import ThemePicker from './ThemePicker' Vue.component('ThemePicker', ThemePicker)
第二步, 放置于 App.vue
中
<template> <div id="app"> <el-button type="primary">按鈕</el-button> <el-divider/> //放置動(dòng)態(tài)主題按鈕 <ThemePicker></ThemePicker> </div> </template> <script> export default { } </script> <style> </style>
使用vue-element-admin模板二次開(kāi)發(fā)的都可以使用
多語(yǔ)言實(shí)現(xiàn)
初始化多語(yǔ)言包
使用國(guó)際化 i18n 方案。通過(guò) vue-i18n而實(shí)現(xiàn)。
第一步,我們需要首先國(guó)際化的包
npm i vue-i18n@8
第二步,需要單獨(dú)一個(gè)多語(yǔ)言的實(shí)例化文件 src/lang/index.js
import Vue from 'vue' // 引入Vue import VueI18n from 'vue-i18n' // 引入國(guó)際化的包 import elementEN from 'element-ui/lib/locale/lang/en' // 引入餓了么的英文包 import elementZH from 'element-ui/lib/locale/lang/zh-CN' // 引入餓了么的中文包 Vue.use(VueI18n) // 全局注冊(cè)國(guó)際化包 export default new VueI18n({ locale: 'zh', // 從cookie中獲取語(yǔ)言類型 獲取不到就是中文 messages: { en: { ...elementEN // 將餓了么的英文語(yǔ)言包引入 }, zh: { ...elementZH // 將餓了么的中文語(yǔ)言包引入 } } })
上面的代碼的作用是將Element的兩種語(yǔ)言導(dǎo)入了
第三步,在main.js中對(duì)掛載 i18n的插件,并設(shè)置element為當(dāng)前的語(yǔ)言
import Vue from 'vue' import ElementUI from 'element-ui' import locale from 'element-ui/lib/locale/lang/en' // 設(shè)置element為當(dāng)前的語(yǔ)言 import i18n from '@/lang/index' Vue.use(ElementUI, { locale }) new Vue({ el: '#app', i18n, render: h => h(App) })
引入自定義語(yǔ)言包
此時(shí),element已經(jīng)變成了zh,也就是中文,但是我們常規(guī)的內(nèi)容怎么根據(jù)當(dāng)前語(yǔ)言類型顯示?
這里,針對(duì)英文和中文,我們可以自己封裝不同的語(yǔ)言包 src/lang/zh.js , src/lang/en.js src/lang/zh.js
export default { lang: { dashboard: '首頁(yè)', bug:'bug就是bug' } }
src/lang/en.js
export default { lang: { dashboard: 'Dashboard', bug:'bug is a bug' } }
第四步,在index.js中同樣引入該語(yǔ)言包
import customZH from './zh' // 引入自定義中文包 import customEN from './en' // 引入自定義英文包 Vue.use(VueI18n) // 全局注冊(cè)國(guó)際化包 export default new VueI18n({ locale: 'zh', // 從cookie中獲取語(yǔ)言類型 獲取不到就是中文 messages: { en: { ...elementEN, // 將餓了么的英文語(yǔ)言包引入 ...customEN }, zh: { ...elementZH, // 將餓了么的中文語(yǔ)言包引入 ...customZH } } })
自定義語(yǔ)言包的內(nèi)容怎么使用?
第五步,在App.vue中應(yīng)用
當(dāng)我們?nèi)肿?cè)i18n的時(shí)候,每個(gè)組件都會(huì)擁有一個(gè) $t
的方法,它會(huì)根據(jù)傳入的key,自動(dòng)的去尋找當(dāng)前語(yǔ)言的文本,我們可以將左側(cè)菜單變成多語(yǔ)言展示文本
App.vue
<template> <div id="app"> <h1 v-text="$t('lang.dashboard')"></h1> <h1 v-text="$t('lang.bug')"></h1> </div> </template>
注意:當(dāng)文本的值為嵌套時(shí),可以通過(guò) $t('key1.key2.key3...')
的方式獲取
現(xiàn)在已經(jīng)完成了多語(yǔ)言的接入,接下來(lái)封裝切換多語(yǔ)言的組件
封裝多語(yǔ)言插件
第六步,封裝多語(yǔ)言組件 src/components/lang/index.vue
<template> <el-dropdown trigger="click" @command="changeLanguage"> <!-- 這里必須加一個(gè)div --> <div> <el-button type="primary">切換多語(yǔ)言</el-button> </div> <el-dropdown-menu slot="dropdown"> <el-dropdown-item command="zh" :disabled="'zh'=== $i18n.locale ">中文</el-dropdown-item> <el-dropdown-item command="en" :disabled="'en'=== $i18n.locale ">en</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </template> <script> export default { methods: { changeLanguage(lang) { this.$i18n.locale = lang // 設(shè)置給本地的i18n插件 this.$message.success('切換多語(yǔ)言成功') } } } </script>
第七步,在App.vue中引入
<!-- 切換多語(yǔ)言 --> <template> <div id="app"> <lang/> <el-divider/> <el-divider/> <el-divider/> <h1 v-text="$t('lang.dashboard')"></h1> <h1 v-text="$t('lang.bug')"></h1> </div> </template>
總結(jié)
到此這篇關(guān)于vue中elementUI里面一些插件的使用的文章就介紹到這了,更多相關(guān)vue elementUI插件使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 綁定對(duì)象,數(shù)組之?dāng)?shù)據(jù)無(wú)法動(dòng)態(tài)渲染案例詳解
這篇文章主要介紹了vue 綁定對(duì)象,數(shù)組之?dāng)?shù)據(jù)無(wú)法動(dòng)態(tài)渲染案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09vue-router之實(shí)現(xiàn)導(dǎo)航切換過(guò)渡動(dòng)畫(huà)效果
今天小編就為大家分享一篇vue-router之實(shí)現(xiàn)導(dǎo)航切換過(guò)渡動(dòng)畫(huà)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10vue移動(dòng)端項(xiàng)目渲染pdf步驟及問(wèn)題小結(jié)
這篇文章主要介紹了vue移動(dòng)端項(xiàng)目渲染pdf步驟,vue-pdf的插件在使用的過(guò)程中是連連踩坑的,基本遇到3個(gè)問(wèn)題,分別在文中給大家詳細(xì)介紹,需要的朋友可以參考下2022-08-08Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方
這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)同樣遇到這樣問(wèn)題的朋友具有一定的需要的朋友可以參考下2023-02-02vue使用echarts詞云圖的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于vue使用echarts詞云圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Vue3.0中的monorepo管理模式的實(shí)現(xiàn)
這篇文章主要介紹了Vue3.0中的monorepo管理模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Vue Treeselect樹(shù)形下拉框的使用小結(jié)
樹(shù)形下拉框是一個(gè)帶有下列樹(shù)形結(jié)構(gòu)的下拉框,本文主要介紹了Vue Treeselect樹(shù)形下拉框的使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10