Appearance
Making components
Components allow us to split the website into independent and reusable pieces, and think about each piece in isolation. It's common for an app to be organized into a tree of nested components: Nuxt is amazing with components, as unlike Vue or Next.js, components are automatically imported if you place them in the
/components
folder.
Today, we will be making our navigation bar into a component, so that we can reuse it for our future pages.
Instructions
- Create a new folder by running this in the terminal
bash
mkdir components
You'll see a new folder components
appear.
INFO
Make sure that before you run the command, npm run dev
isn't running. If it's running, you will be able to use the terminal as normal but no commands will be executed. Press Ctrl+C to stop npm, then run your command.
Right click on the
components
folder and selectCreate Vue Component
. Name itnavbar
. Expand thecomponents
folder and opennavbar.vue
Now, we'll have to copy the
<header>
component into ournavbar.vue
. Inindex.vue
, scroll all the way up and hover right beside line 3 on the left side. You'll see an arrow like 'V'. Click on it to collapse on the<header>
component. Cut the entire componentGo into
navbar.vue
and paste your component in between<div>
vue
<template>
<div>
<!-- put your component here -->
</div>
</template>
<script lang="ts" setup>
</script>
<style>
</style>
- Now go back to
index.vue
and then add
vue
<Navbar />
in between where the <header>
used to be.
- You will realise that there are errors in our
navbar.vue
. We need to add the const for ournavbar.vue
, so go toindex.vue
and scroll down until you see<script setup>
. Cut everything from there excluding the<script setup>
tags and paste it intonavbar.vue
's<script lang="ts" setup>
vue
<template>
<div>
<!-- your component -->
</div>
</template>
<script lang="ts" setup>
/* the script you copied goes here */
</script>
<style>
</style>
To check if you've done it correctly, run
bash
npm run dev
You should get the same page as you did before you set up your component.
If you need a tutorial, do watch this short tutorial to help you out