VUE 學(xué)習(xí)總結(jié)之簡(jiǎn)單的Rate評(píng)分組件

2018-7-9    seo達(dá)人

如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里

說(shuō)明

本組件基于element-ui 的圖標(biāo)庫(kù)(星星圖標(biāo))

第一步:

vue + webpack + element-ui 框架

第二步:

創(chuàng)建Rate.vue文件,實(shí)現(xiàn)雙向綁定分?jǐn)?shù)

第三部:

使用組件

代碼

在app.vue中引入組件


    
  1. <Rate v-model='value' size="32px">
  2. <span>{{value}} 分</span>
  3. </Rate>
import Rate from './components/Rate'

組件


    
  1. <template>
  2. <div class="Rating" :value='value'>
  3. <ul class="Rating-list">
  4. <li v-for="s in 5" @click="changeRate(s)">
  5. <i :class="s <= star ? 'el-icon-star-on':'el-icon-star-off'" :style='style'></i>
  6. </li>
  7. </ul>
  8. <slot></slot> <!--顯示用戶自定義內(nèi)容-->
  9. </div>
  10. </template>


    
  1. props: {
  2. size: { //父組件傳值設(shè)置字體大小
  3. type: String,
  4. default: '16px'
  5. },
  6. value: { //綁定value,與$emit實(shí)現(xiàn)雙向綁定
  7. type:Number,
  8. default:0
  9. }
  10. },
  11. data() {
  12. return {
  13. star: this.value, // 初始化
  14. style: {
  15. fontSize: this.size //通過(guò)prop傳值設(shè)置星星字體大小
  16. }
  17. }
  18. },
  19. methods: {
  20. changeRate(s) {
  21. this.star = s //更新當(dāng)前星星數(shù)量
  22. this.$emit('input', s); //將當(dāng)前星星數(shù)量s與v-model綁定
  23. }
  24. }

demo演示



分享本文至:

日歷

鏈接

個(gè)人資料

存檔