Last active
July 9, 2025 14:53
-
-
Save MaxMonteil/fc3008bd2717570481bbd9caeb3340c7 to your computer and use it in GitHub Desktop.
Basic Vue interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <h1>List items</h1> | |
| <ul> | |
| <li v-for="item in items" :key="item.id"> | |
| {{ item.name }} | |
| </li> | |
| </ul> | |
| <button @click="addItem">Add item</button> | |
| </template> | |
| <script setup> | |
| import { ref } from 'vue' | |
| const items = ref([ | |
| { name: "Item 1", id: 1 }, | |
| { name: "Item 2", id: 2 }, | |
| { name: "Item 3", id: 3 }, | |
| ]) | |
| function addItem() { | |
| const id = items.length + 1 | |
| items.push({ name: `Item ${id}`, id }) | |
| } | |
| </script> |
Author
Author
With types
<template>
<h1>List items</h1>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
<button @click="addItem">Add item</button>
</template>
<script setup lang="ts">
import { ref } from 'vue'
// 1. Define this type such that given objects will require an `id` parameter
// 2. Make the type of the `id` parameter configurable with a second generic argument
declare type SafeItem<T>;
type ListItem = { name: string }
const items = ref<SafeItem<ListItem>[]>([
{ name: "Item 1", id: 1 },
{ name: "Item 2", id: 2 },
{ name: "Item 3", id: 3 },
])
function addItem() {
const id = items.length + 1
items.push({ name: `Item ${id}`, id })
}
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React version: