Skip to content

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

javascript

Create a reactive counter with ref.

import { ref } from "vue";
const count = ref(0);
count.value++;

2. Computed Full Name

javascript

Compute 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

javascript

Watch a query and fetch results.

watch(query, async (value) => {
  results.value = await search(value);
});

4. v-model Input

html

Bind an input with v-model.

<input v-model="email" type="email" />

5. Emit Event

javascript

Emit a custom event from a child.

const emit = defineEmits(["save"]);
function onSave() {
  emit("save", form.value);
}

6. Props Define

javascript

Define typed props with defineProps.

const props = defineProps({
  title: { type: String, required: true },
  count: { type: Number, default: 0 },
});

7. Provide Inject

javascript

Provide a theme and inject it.

provide("theme", theme);
const theme = inject("theme");

8. List Key

html

Render a keyed list.

<li v-for="item in items" :key="item.id">{{ item.label }}</li>

9. Composable Fetch

javascript

Write 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

javascript

Define a simple Pinia store.

export const useCounterStore = defineStore("counter", {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++;
    },
  },
});

11. Router Navigation

javascript

Navigate programmatically.

const router = useRouter();
router.push({ name: "user", params: { id } });

12. Teleport Modal

html

Teleport a modal to body.

<Teleport to="body">
  <div class="modal">Content</div>
</Teleport>

13. Slot Fallback

html

Provide a default slot fallback.

<slot>
  <p>Default content</p>
</slot>

14. Lifecycle Hook

javascript

Run code on mount.

onMounted(() => {
  console.log("mounted");
});

15. v-memo List

html

Memoize expensive list rows.

<div v-for="item in list" :key="item.id" v-memo="[item.id, item.selected]">
  {{ item.label }}
</div>