vue計算屬性時v-for處理數組時遇到的一個bug問題,vuev-for

來源:互聯網
上載者:User

vue計算屬性時v-for處理數組時遇到的一個bug問題,vuev-for

問題

bug: You may have an infinite update loop in a component render function 無限迴圈

1.需要處理的數組(在 ** ssq **裡):

bonus_code: ['01', '19', '25', '26', '27', '33', '10']

2.計算屬性 computed:

ssqRed: function() {return this.ssq.bonus_code.splice(0, 6)},ssqBlue: function() {return this.ssq.bonus_code.splice(6, 7)}

3.v-for 代碼:

<em class="red-ball tac mr5 fl" v-for="(item, index) in ssqRed">{{ item }}</em><em class="blue-ball tac mr5 fl" v-for="(item, index) in ssqBlue">{{ item }}</em>

4.最終結果我想把數組前6個數渲染成紅色球,最後一個(也就是第7個)渲染成藍色。

解答

我已經在 SegmentFault上提問,地址:vue計算屬性computed同時操作一個數組

我已採納答案,將代碼改成:

ssqRed: function() { return this.ssq.bonus_code.slice(0, 6)},ssqBlue: function() { return this.ssq.bonus_code.slice(6, 7)}

問題就在於自己沒搞清楚 splice會對原數組造成改變。

在尋找解決方案時,朋友少暉教給我一種更好的解決方式,很感謝

即類名判斷

1.如果數組大小已知,就做一個類名判斷,索引大於多少展示藍色的類名就行了;

2.處理後的 html代碼:

<em v-for="(item, index) in ssq.bonus_code" :class="['tac','mr5','fl',index>5?'blue-ball':'red-ball']" >{{ item }}</em>

3.增加的代碼:

index>5?'blue-ball':'red-ball'

總結

以上所述是小編給大家介紹的vue計算屬性時v-for處理數組時遇到的一個bug問題,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.