⊗jsvuPmCmRE 69 of 72 menu

Reactive Editing of Component Data in Vue

Now let's implement editing of data of our child components. First, in the parent component, we will make a method for changing the user by his id:

methods: { change(id, name, surn) { this.users = this.users.map((user) => { if (user.id === id) { user.name = name; user.surn = surn; } return user; }); } }

Let's create components in a loop, passing them the name, last name, id and the method for changing as parameters:

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

Let's register the emitted event in the emits setting of the child component:

props: { id: Number, name: String, surn: String, }, emits: ['change'],

Now in the child component we will create a property that will set the component mode, showing or editing:

data() { return { isEdit: false, } }

Let's also make properties to support the work of inputs for editing:

data() { return { isEdit: false, newName: '', newSurn: '', } }

Let's make it so that the initial values ​​of these properties are taken from props:

data() { return { isEdit: false, newName: this.name, newSurn: this.surn, } }

Let's make a method that will launch the editing mode:

methods: { edit() { this.isEdit = true; } }

Let's create a method that will save edited data while disabling editing mode:

methods: { save() { this.isEdit = false; this.$emit('change', this.id, this.newName, this.newSurn); } }

Let's make a view of the child component:

<template> <template v-if="!isEdit"> {{ name }} {{ surn }} <button @click="edit"> edit </button> </template> <template v-else> <input v-model="newName"> <input v-model="newSurn"> <button @click="save"> save </button> </template> </template>

Implement editing of data of the Employee component.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline