Quiz: Too Early
The search field should have the cursor in it when the page opens. Instead, the page is empty, and the console says TypeError: Cannot read properties of null (reading 'focus'). Click the line with the mistake.
1<script setup>
2import { computed, onMounted, ref, useTemplateRef } from "vue";
3
4const menu = ref([]);
5const query = ref("");
6const search = useTemplateRef("search");
7const shown = computed(() => {
8 const searchText = query.value.toLowerCase();
9 return menu.value.filter((item) =>
10 item.name.toLowerCase().includes(searchText),
11 );
12});
13
14search.value.focus();
15
16onMounted(async () => {
17 const response = await fetch("menu.json");
18 menu.value = await response.json();
19});
20</script>
21
22<template>
23 <h1>Corner Café</h1>
24 <input ref="search" v-model="query" placeholder="Search the menu">
25 <ul>
26 <li v-for="item in shown" :key="item.id">{{ item.name }}: €{{ item.price }}</li>
27 </ul>
28</template>