...

/

The Single Page and Route Middleware

The Single Page and Route Middleware

Discover how to effectively run middleware on selected routes, and learn about the role of the “/single” route.

The modal has a NuxtLink component, which has two tasks. It redirects us to the “/single” URL and also sets the selected image to our state:

Press + to interact
<!-- components/Modal.vue -->
<NuxtLink to="/single" @click="selectedImage = modalImage"
>More info</NuxtLink
>

Creating the single page

This new route now needs to be handled by adding a new page alongside the existing index.vue:

Press + to interact
Pages directory structure
Pages directory structure

Let’s look at the following code:

Press + to interact
<!-- pages/single.vue -->
<script setup>
const selectedImage = useSelectedImage();
</script>
<template>
<div>
<img
:src="selectedImage.largeImageURL"
:alt="`image by ${selectedImage.user}`"
/>
<p>By: {{ selectedImage.user }}</p>
<p>Tags: {{ selectedImage.tags }}</p>
</div>
</template>
  • Line 4: We store the selected image information from the useState.js file.

  • Line 9–12: ...