Implement a list component in Vue that renders a dynamic list of items, with functionality to add/remove items.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
<template>
  <div>
    <input v-model="newItem" @keyup.enter="addItem" />
    <button @click="addItem">Add</button>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">Remove</button>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const items = ref(['Apple', 'Banana']);
const newItem = ref('');

const addItem = () => {
  if (newItem.value) {
    items.value.push(newItem.value);
    newItem.value = '';
  }
};

const removeItem = (index) => {
  items.value.splice(index, 1);
};
</script>