1. 程式人生 > 程式設計 >淺談Vue使用Elementui修改預設的最快方法

淺談Vue使用Elementui修改預設的最快方法

相信大家都需要過,在Vue中使用Elementui的時候,遇到最多也最蛋疼的問題就是修改預設樣式,接下來直奔主題;

// template
 <el-progress 
 :text-inside="true" 
 :stroke-width="26" 
 :percentage="70"
 ></el-progress>

預設樣式

淺談Vue使用Elementui修改預設的最快方法

方法1

1、找預設新增的類名

淺談Vue使用Elementui修改預設的最快方法

2、去掉scoped,scoped是Vue是限制獨立元件中的CSS樣式不被溢位到全域性使用!

// style
.el-progress-bar__inner{
 background: #000 ;
}
// 這兩種酌情使用。
.el-progress-bar__inner{
 background: #000 !important;
}
// !important是css選擇器中的屬性,預設權重無線大!

總結:這種方法會生效,但是會影響到全域性;

淺談Vue使用Elementui修改預設的最快方法

方法2,

使用Vue中的深度作用域選擇器! 這個符號哦 >>>

<style scoped>
>>> .el-progress-bar__inner{
 background: #000 ;
}
</style>

總結:使用Vue的深度選擇器,就可以完美的解決!

淺談Vue使用Elementui修改預設的最快方法

注意:有些像 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,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修改預設的最快方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。