⊗jsvuPmCmLC 63 of 72 menu

Creating Components in the Loop in Vue

Components can be created in a loop, passing them the appropriate data. Let's see how this is done. Let's say the parent component is given the following array of objects:

data() { return { users: [ { id: 1, name: 'name1', surn: 'surn1' }, { id: 2, name: 'name2', surn: 'surn2' }, { id: 3, name: 'name3', surn: 'surn3' }, ], } }

Let's iterate over this array of objects in a loop and create components with users in the loop:

<template> <User v-for="user in users" /> </template>

Let's specify the values ​​of the key attribute for the correct operation of the loop:

<template> <User v-for="user in users" :key="user.id" /> </template>

We will also pass their data, the first and last name of each user, to the components:

<template> <User v-for="user in users" :name="user.name" :surn="user.surn" :key="user.id" /> </template>

The following array of employee data is given:

data() { return { users: [ { id: 1, name: 'name1', salary: 100, age: 30, }, { id: 2, name: 'name2', salary: 200, age: 40, }, { id: 3, name: 'name3', salary: 300, age: 50, }, ], } }

Using a loop, create Employee components based on this array, passing them the appropriate data.

enru