Coding practice
Vue.js Coding Questions
15 hands-on challenges with prompts and solution sketches for Vue.js interview rounds.
Challenge set
15 coding questions
1. Ref Counter
javascriptCreate a reactive counter with ref.
import { ref } from "vue";
const count = ref(0);
count.value++; 2. Computed Full Name
javascriptCompute a full name from refs.
import { computed, ref } from "vue";
const first = ref("Ada");
const last = ref("Lovelace");
const fullName = computed(() => `${first.value} ${last.value}`); 3. Watch Effect
javascriptWatch a query and fetch results.
watch(query, async (value) => {
results.value = await search(value);
}); 4. v-model Input
htmlBind an input with v-model.
<input v-model="email" type="email" /> 5. Emit Event
javascriptEmit a custom event from a child.
const emit = defineEmits(["save"]);
function onSave() {
emit("save", form.value);
} 6. Props Define
javascriptDefine typed props with defineProps.
const props = defineProps({
title: { type: String, required: true },
count: { type: Number, default: 0 },
}); 7. Provide Inject
javascriptProvide a theme and inject it.
provide("theme", theme);
const theme = inject("theme"); 8. List Key
htmlRender a keyed list.
<li v-for="item in items" :key="item.id">{{ item.label }}</li> 9. Composable Fetch
javascriptWrite a useFetch composable.
export function useFetch(url) {
const data = ref(null);
const error = ref(null);
onMounted(async () => {
try {
data.value = await (await fetch(url)).json();
} catch (e) {
error.value = e;
}
});
return { data, error };
} 10. Pinia Store Stub
javascriptDefine a simple Pinia store.
export const useCounterStore = defineStore("counter", {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++;
},
},
}); 11. Router Navigation
javascriptNavigate programmatically.
const router = useRouter();
router.push({ name: "user", params: { id } }); 12. Teleport Modal
htmlTeleport a modal to body.
<Teleport to="body">
<div class="modal">Content</div>
</Teleport> 13. Slot Fallback
htmlProvide a default slot fallback.
<slot>
<p>Default content</p>
</slot> 14. Lifecycle Hook
javascriptRun code on mount.
onMounted(() => {
console.log("mounted");
}); 15. v-memo List
htmlMemoize expensive list rows.
<div v-for="item in list" :key="item.id" v-memo="[item.id, item.selected]">
{{ item.label }}
</div>