<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h3>Vue Js Add JSON Data to Array</h3>
<input v-model="data.name" type="text" placeholder="Enter name" />
<input v-model="data.role" type="text" placeholder="Enter role" />
<input v-model="data.email" type="email" placeholder="Enter email" />
<button @click="addData">Add Data</button>
<ul>
<li v-for="(item, index) in dataArray" :key="index">{{ item }}</li>
</ul>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
data: {
name: 'John',
role: 'Developer',
email: 'john@gmail.com'
},
dataArray: []
};
},
methods: {
addData() {
this.dataArray.push({ ...this.data });
this.data.name = '';
this.data.role = '';
this.data.email = '';
}
}
});
</script>
<style scoped>
#app {
margin: 20px auto;
font-family: Arial, sans-serif;
width:600px
}
h3 {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
input {
margin-bottom: 10px;
padding: 8px;
width: 100%;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
margin-top: 10px;
padding: 0;
}
li {
margin-bottom: 5px;
background-color: #f9f9f9;
padding: 10px;
border-radius: 4px;
}
</style>
</body>
</html>