As your Vue.js Single Page Applications (SPAs) become moderately complex, you start to need Vue Router, and, moreover, nested routes. Nested routes allow for more complex user interfaces with components nested inside each other.
In this article, you will build out an example Vue.js project that highlights the utility of nested routes.
To complete this tutorial, you will need:
This tutorial was verified with Node v16.5.0, npm
v7.20.0, vue
v2.6.14, and vue-router
v3.5.2.
To quickly set up the project, this article will recommend using @vue/cli
.
Note: This article will take the approach of using npx
to avoid a global installation of @vue/cli
;
When prompted with configurations options, you may wish to Manually select features and include Router.
Then, navigate to the newly created project directory:
If at this point you did not install Vue Router at creation, you can add it to your project now.
The following CSS will help us with positioning elements for our user interface.
Under the 'public
directory, create a grid.css
file:
We’ll be making use of CSS grid.
Add grid.css
to index.html
:
Next, let’s make some changes to the default files that @vue/cli
created.
Open App.vue
in your code editor. And make the following modifications to the HTML markup in App.vue
:
And CSS styling in App.vue
:
Save the changes to your files.
Then run the following command in your terminal:
Visit localhost:8080
in your web browser and observe the grid layout.
Now we can begin routing.
@vue/cli
will build a main.js
file:
And a router/index.js
file:
And Home.vue
and About.vue
files in the views
directory.
Let’s revisit App.vue
and change the anchor tag for About to a <router-link>
tag with an attribute of to="/about"
. Then, change the second div
to a <router-view>
tag. Make sure to keep the grid positioning class attributes intact.
<template>
<div id="app">
<h1 class="row1 col12">Example</h1>
<router-link to="/about" class="row1 col3">About</router-link>
<a class="row1 col4">Nested Pages</a>
<router-view class="row2 col234" />
</div>
</template>
We now have a functional site skeleton with routing handled for the About page.
We’re focusing on the routing functionality here so we’re not going to get fancy with styling. Even so, the Nested
page is going to look more elaborate.
To start, create a NestedPages.vue
file in the views
directory:
<template>
<div id="nested">
<h2 class="row1">Nested Pages</h2>
</div>
</template>
<script>
export default {
name: 'NestedPages'
}
</script>
<style scoped>
div {
text-align: center;
}
</style>
And reference it in router/index.js
:
Also, create the following two components which will ultimately be nested in NestedPages.vue
.
Create a NestedPageOne.vue
file:
<template>
<div>
<p>Nested Page One</p>
</div>
</template>
<script>
export default {
name: 'NestedPageOne'
}
</script>
<style scoped>
</style>
And create a similiar NestedPageTwo.vue
file:
<template>
<div>
<p>Nested Page Two</p>
</div>
</template>
<script>
export default {
name: 'NestedPageTwo'
}
</script>
<style scoped>
</style>
Finally, revisit App.vue
and update the Nested Pages link to use <router-link>
:
<template>
<div id="app">
<h1 class="row1 col12">Example</h1>
<router-link to="/about" class="row1 col3">About</router-link>
<router-link to="/nested" class="row1 col4">Nested Pages</router-link>
<router-view class="row2 col234" />
</div>
</template>
At this point, you have a new nested
route and two new components.
Revisit NestedPages.vue
and add <router-link>
for Nested Page One and Nested Page Two.
<template>
<div id="nested">
<h2 class="row1">Nested Pages</h2>
<div class="flex-container row2">
<router-link to="/nested/one">Nested Page One</router-link>
<router-link to="/nested/two">Nested Page Two</router-link>
</div>
<router-view class="row3" />
</div>
</template>
<script>
export default {
name: 'NestedPages'
}
</script>
<style scoped>
div {
text-align: center;
}
#nested {
display: grid;
grid-template-rows: 20% 40% 40%;
}
.flex-container {
display: flex;
justify-content: space-around;
}
</style>
Now, let’s update router/index.js
to reference those nested routes using children
.
Note: Note that the nesting of children can continue infinitely.
Save the changes to your files.
Then run the following command in your terminal:
Revisit localhost:8080
in your web browser. When you visit the /nested
route, you can visit the NestedPageOne
and NestedPageTwo
children routes.
In this article, you built out an example Vue.js project that highlights the utility of nested routes.
Other things to keep in mind on the topic — we could have routes defined with dynamic segments such as path: '/location/:id'
. Then, on the views for those routes, we can reference that id as this.$route.params
. This is useful when you have more data of a particular type (users, pictures, etc.) that you are wishing to display on your site or app.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!