Vue में डेटा जोड़ने के लिए कंपोनेंट

चलिए अब नए उपयोगकर्ताओं को जोड़ने के लिए एक फॉर्म लागू करते हैं। यह फॉर्म हम एक अलग कंपोनेंट के रूप में बनाएंगे। आइए लागू करना शुरू करते हैं। सबसे पहले पैरेंट कंपोनेंट में उपयोगकर्ता जोड़ने के लिए एक मेथड बनाते हैं:

App.vuemethods: {
  add(name, surn) {
    let id = this.users.length + 1;
    
    this.users.push({
      id,
      name,
      surn
    });
  }
}

पैरेंट कंपोनेंट के व्यू में चाइल्ड कंपोनेंट को इंपोर्ट करते हैं:

App.vue<template>
  <UserForm @add="add" />
</template>

चाइल्ड कंपोनेंट में इनपुट्स को कंट्रोल करने के लिए प्रॉपर्टीज बनाते हैं:

components/UserForm.vuedata() {
  return {
    newName: '',
    newSurn: '',
  }
}

चाइल्ड कंपोनेंट में डेटा सेव करने के लिए एक मेथड बनाते हैं:

components/UserForm.vuemethods: {
  save() {
    this.$emit('add', this.newName, this.newSurn);
  }
}

चाइल्ड कंपोनेंट का व्यू बनाते हैं:

components/UserForm.vue<template>
  <input v-model="newName">
  <input v-model="newSurn">
  
  <button @click="save">
    save
  </button>
</template>
Задание 1

नए कर्मचारी को जोड़ने के लिए एक फॉर्म लागू करें।