screen_rotation
Copied to Clipboard
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h3>Vue Js Create Array of Object Dynamically</h3> <form @submit.prevent="addObject"> <div> <label>Name:</label> <input v-model="name" required> </div> <div> <label>Age:</label> <input v-model.number="age" type="number" required> </div> <div> <button type="submit">Add Object</button> </div> </form> <ul> <li v-for="(obj, index) in objects" :key="index"> Object {{ index + 1 }} - Name: {{ obj.name }}, Age: {{ obj.age }} </li> </ul> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { objects: [], // initial empty array name: '', age: '' }; }, methods: { addObject() { // create a new object with input values const newObj = { name: this.name, age: this.age }; // add the object to the array this.objects.push(newObj); // clear the input fields this.name = ''; this.age = ''; } } }); </script> <style scoped> #app { font-family: Arial, sans-serif; padding: 20px; } h3 { font-size: 24px; font-weight: bold; margin-bottom: 20px; } form { display: flex; flex-direction: column; margin-bottom: 20px; } label { font-weight: bold; margin-bottom: 5px; } input { padding: 10px; border-radius: 5px; border: 1px solid #ccc; margin-bottom: 10px; } button { background-color: #007bff; color: #fff; border: none; border-radius: 5px; padding: 10px 20px; cursor: pointer; } button:hover { background-color: #0062cc; } ul { list-style: none; margin: 0; padding: 0; } li { margin-bottom: 10px; } </style> </body> </html>