tuihou123321
V2EX  ›  Vue.js

vue 渲染的一些问题,v-for 要求渲染多次,最后只渲染一次?

  •  
  •   tuihou123321 · Oct 3, 2017 · 4351 views
    This topic created in 3170 days ago, the information mentioned may be changed or developed.

    完整代码地址: https://github.com/tuihou123321/gitTest/blob/master/projects/elemeAll-html/home.html

    图片效果: https://sfault-image.b0.upaiyun.com/192/804/1928048893-59d33bba2e3f4_articlex

    html 模板代码

     <div class="starBox">
        <span class="grey">
            <i class="fa fa-star" v-for="n in 5"></i>
        </span>
        <span style="color:#ff9a0d;">
            <i class="fa fa-star" v-for="n in starFun(item,0)"></i>
            <i class="fa fa-star-half" v-for="m in starFun(item,1)"></i>
        </span>
    </div>
    

    vue 中相应方法代码

    
        methods:{               
            starFun:function(item,index){
                var num=item.rating+"";
                var a;
                if(index==0){
                    if(num.indexOf(".")<0){
                        a=num;
                    }
                    else{
                        a=num.split(".")[0];
                    }
                }
                else{
                    if(num.indexOf(".")<0 ||  num.split(".")[1]<5 ){
                        a=0;
                    }
                    else{
                        a=1;
                    }
                }
                console.log(a);
                return a;
            }
        }
        
        ```
    
    2 replies    2017-10-03 16:21:52 +08:00
    zhlssg
        1
    zhlssg  
       Oct 3, 2017 via iPhone
    感觉 startFun 放到 computed 里好一点吧
    binux
        2
    binux  
       Oct 3, 2017 via Android
    你干嘛要 return 个字符串?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1097 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:59 · PVG 06:59 · LAX 15:59 · JFK 18:59
    ♥ Do have faith in what you're doing.