Nuxt.js
Getting Started with Nuxt.js
Updated: 03 September 2023
Introduction
Nuxt.js is server-side framework based on Vue, the content here is kind of from this DesignCourse Video and this video but with TypeScript. The overall code from the video can be found here
Getting Started
To create a new app you can run:
You can then just retain the default options for the most part, at this point you will also be able to select TypeScript as the language
That will initialize a new Nuxt app in the my-app
folder. You can then do the following to start the application:
Generated Files
Once we have run the project creation we will see the following folders in our application:
assets
components
layouts
middleware
pages
plugins
static
store
We also have the layouts/default.vue
which has our basic page layout:
In here we can see the section which will render our nuxt
component which is the root for our Nuxt application
Creating a Layout
We’ll create some partials for our layout in a layout/partials
directory
For a basic layout we can create a nav section:
partials/nav.vue
Note how we make use of nuxt-link
instead of the normal a
. We also use Vue.extend
so that we can allow typescript to infer the type correctly
Next, we can import and use the Nav
component in our default layout like so:
layout/default.vue
Including Global Resources
To include external resources or do anything that we would normally do in our head
element in HTML, we can do via the nuxt.config.js
file. For example if we would like to add a link to a font we can do so by adding a link
to our head
nuxt.config.js
If we would like to include a css
file we have locally we can also do this in the same config file by referencing the path like @assets/styles/main.css
Pages
Nuxt makes use of file based routing, based on this our main page for our site will be located in the pages/index.vue
file. We can clean up the unecessary content in this file and just leave the following:
Each page
component will essentially be rendered into the nuxt
element. We can create another page for about
and include some content that’s similar to above. This file will be called pages/about.vue
Once we have the about
page we should be able to click on our links and view the different pages
Page Metadata
If we would like to provide some metadata we can export a head
function from our component in which we specify some metadata. We need to do this in the component’s script
tag:
pages/about.vue
Note that if we intend to use our metadata like we do above we need to be sure to remove the relevant meta from out nuxt.config.js
as well
Router Transitions
Nuxt also has built-in router transitions, these make use of the page-enter-active
and page-leave-active
classes to apply the transitions. We can update our layout/default.vue
file with an animation to apply it
layout/default.vue
We can also apply transitions to specific elements on a page by exporting a transition
property with the name of the transition, and including the CSS for an animation. If we create a transition like transition: 'floop'
, we will need to have the CSS transitions defined like aboe but using the class names floop-enter-active
and floop-leave-active
so they can be applied
Retrieve Data
We can retrieve data on the client or on the server using the fetch
method on a component
When using fetch
in Nuxt we need to take note of the following:
- Set the initial data with the
data
function ordata
property - Set
fetchOnServer
totrue
to allow for server side fetching if on the server - We can optionally set the
fetchDelay
to delay the fetch on the client, e.g. to prevent flashing. If we want everything to load immediately then don’t set this - Use
isomorphic-fetch
to allow for consistent fetching on both the client and server - Set the relevant data in the
fetch
function
The code for our component will look like so:
pages/index.vue
By using the fetch
method for the component we get the ability to view the current state of the fetch
when on the client, we can use the $fetchState
values for $fetchState.error
, fetchState.pending
, and $fetchState.timestamp
for the status of the fetch. We can also call the hook from a component using the $fetch
function in a template
Make sure the data model in your
data
function aligns with the populated data on thefetch
function otherwise the SSR will not kick in and it will render on the client
We can render the component with the awareness of the fetch
state like so:
pages/index.vue