PHP前端开发

vue数组怎么遍历数组

百变鹏仔 3周前 (09-25) #VUE
文章标签 数组

vue 中遍历数组

在 Vue.js 中,可以使用 v-for 指令遍历数组。该指令允许您为数组中的每个元素渲染一个特定的模板。

语法:

<template v-for="item in items"><!-- 模板内容 --></template>

参数:

示例:

立即学习“前端免费学习笔记(深入)”;

<template><ul><li v-for="item in items">{{ item }}</li>  </ul></template><script>import { ref } from 'vue';export default {  setup() {    const items = ref(['Apple', 'Banana', 'Orange']);    return {      items,    };  },};</script>

在这个示例中,v-for 指令用于遍历 items 数组,并为每个数组元素渲染一个

  • 元素。

    其他功能:

    更多示例:

    <template v-for="item in items"><h1>{{ item.name }}</h1>  <p>{{ item.age }}</p></template>
    <template v-for="(item, index) in items"><p>{{ index + 1 }}. {{ item }}</p></template>