淺談Vue使用Elementui修改默認的最快方法
相信大家都需要過,在Vue中使用Elementui的時候,遇到最多也最蛋疼的問題就是修改默認樣式,接下來直奔主題;
// template <el-progress :text-inside='true' :stroke- :percentage='70' ></el-progress>
默認樣式
方法1
1、找默認添加的類名
2、去掉scoped,scoped是Vue是限制獨立組件中的CSS樣式不被溢出到全局使用!
// style.el-progress-bar__inner{ background: #000 ;}// 這兩種酌情使用。.el-progress-bar__inner{ background: #000 !important;}// !important是css選擇器中的屬性,默認權重無線大!
總結:這種方法會生效,但是會影響到全局;
方法2,
使用Vue中的深度作用域選擇器! 這個符號哦 >>>
<style scoped>>>> .el-progress-bar__inner{ background: #000 ;}</style>
總結:使用Vue的深度選擇器,就可以完美的解決!
注意:有些像 Sass 之類的預處理器無法正確解析 >>>。
這種情況下你可以使用 /deep/ 或 ::v-deep 操作符取而代之——兩者都是 >>> 的別名,同樣可以正常工作。
給大家附上官網地址:https://vue-loader.vuejs.org/zh/guide/scoped-css.html#混用本地和全局樣式
補充知識:Vue Element Upload組件自定義上傳行為及值回填
問題
由于項目使用element-ui,然后upload默認上傳方式不支持我們現有接口。參照了一下官方API及相關博客,解決了我現有問題。
解決方式
自定義上傳:upload組件提供了一個http-request屬性,官方給的描述是:覆蓋默認的上傳行為,可以自定義上傳的實現
值的回填:upload組件提供了一個file-list屬性,描述:上傳的文件列表
#具體代碼實現
自定義上傳行為
這里使用圖片上傳作為實例
template部分
<el-upload action='https://up-z2.qbox.me' list-type='picture-card' :http-request='uploadImg' :on-success='uploadImgSuccess' :on-remove='handleRemove'> <i class='el-icon-plus'></i></el-upload>
以上是template部分,我們實現了http-request, on-success, on-remove三個屬性
script部分
methods: { uploadImg (f) { this.axios.get(’./getToken’).then((response) => {//獲取token let param = new FormData(); //創建form對象 param.append(’file’,f.file);//通過append向form對象添加數據 param.append(’token’,response.data.token);//通過append向form對象添加數據 param.append(’key’,response.data.key);//添加form表單中其他數據 let config = { headers:{’Content-Type’:’multipart/form-data’} }; //添加請求頭 this.axios.post(f.action,param,config)//上傳圖片 .then(response=>{ f.onSuccess(response.data) }) .catch(({err}) => { f.onError() }) }) .catch(() => { f.onError() }) }, uploadImgSuccess(response, file, fileList) { // 緩存接口調用所需的文件路徑 console.log(’文件上傳成功’) }, handleRemove(file, fileList) { // 更新緩存文件 console.log(’文件刪除’) }}
值回填
同樣以圖片上傳為例
template部分
<el-upload action='https://up-z2.qbox.me' list-type='picture-card' :http-request='uploadImg' :on-remove='handleRemove' :on-change='handleImgChange' :file-list='imgList'> <i class='el-icon-plus'></i> </el-upload>
script部分
data() { return { imgList: [{url: ’初始需回填的圖片url’, status: ’finished’}] }},methods: { uploadImg (f) { this.axios.get(’./getToken’).then((response) => {//獲取token let param = new FormData(); //創建form對象 param.append(’file’,f.file);//通過append向form對象添加數據 param.append(’token’,response.data.token);//通過append向form對象添加數據 param.append(’key’,response.data.key);//添加form表單中其他數據 let config = { headers:{’Content-Type’:’multipart/form-data’} }; //添加請求頭 this.axios.post(f.action,param,config)//上傳圖片 .then(response=>{ f.onSuccess(response.data) }) .catch(({err}) => { f.onError() }) }) .catch(() => { f.onError() }) }, handleImgChange (file, fileList) {// 這里可以打印file查看數據結構 if (file.response) {//判斷是否上傳成功 this.imgList.push({url: this.tools.cdn(file.response.key), status: ’finished’})//上傳成功之后把值添加到imglist中 } }, handleRemove (file, fileList) {// 這里可以打印filelist查看數據結構 this.imgList = fileList//刪除某張圖片時重新對imglist賦值 }}
寫在最后
一直想把這個記下來,比較懶惰一看好久沒有寫博客了。由于是在我們工程里改的,暫時還沒有寫demo。如有問題,請大家指教
以上這篇淺談Vue使用Elementui修改默認的最快方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: