vue3使用v-for创建一个div列表每个div中显示的内容通过计算属性展示序号的平方
可以使用如下代码实现:
<template>
<div>
<div v-for="(num, index) in squares" :key="index">
{{ num }}
</div>
</div>
</template>
<script>
export default {
computed: {
squares() {
return Array.from({ length: 10 }, (_, index) => Math.pow(index, 2));
},
},
};
</script>
在上面的代码中,我们使用了v-for指令来循环创建div列表,列表中的每个div都会显示计算属性squares中对应序号的平方值。在计算属性中,我们使用了Array.from方法来创建一个长度为10的数组,然后通过箭头函数和Math.pow方法来计算每个元素的平方值。最后,我们将计算出的结果返回作为计算属性的值。
原文地址: https://www.cveoy.top/t/topic/gdXP 著作权归作者所有。请勿转载和采集!