V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tuihou123321
V2EX  ›  Vue.js

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

  •  
  •   tuihou123321 · 2017-10-03 15:45:41 +08:00 · 3779 次点击
    这是一个创建于 2406 天前的主题,其中的信息可能已经有所发展或是发生改变。

    完整代码地址: 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 条回复    2017-10-03 16:21:52 +08:00
    zhlssg
        1
    zhlssg  
       2017-10-03 16:17:34 +08:00 via iPhone
    感觉 startFun 放到 computed 里好一点吧
    binux
        2
    binux  
       2017-10-03 16:21:52 +08:00 via Android
    你干嘛要 return 个字符串?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2532 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:49 · PVG 16:49 · LAX 01:49 · JFK 04:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.