Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

vue 循环列表随机背景颜色如何固定住??

<div class="website-item" v-for="(v,k) in data" :key="k">
    <text :style="{'color':color()}">随机颜色</text>
</div>
methods: {
     color()
    {
        var colorStr = "";
        var randomArr = ["0","1","2","3","4","5", "6","7", "8","9","a", "b","c","d","e","f"]; //产生一个六位的字符串
        for (var i = 0; i < 6; i++) {
            colorStr += randomArr[Math.ceil(Math.random() * (15 - 0) + 0)];
        }
        return "#" + colorStr;
    },
}

如果是静态数据 以上方法基本上没什么大问题,但是 data的数据会加载第二页,当data发生变化时,之前获取的颜色页会随之变动。

我想要的效果是加载更多出来的列表随机给上颜色,并且加载过的不会变。。。如何避免之前加载过的颜色重新更新???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

那你就不要这么写啊……

你往你这个 datapush 数据的时候就把 color 算好当一个属性存下来。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...