Adding Plugins
In Nuxt, you can define third-party scripts or your own scripts and functionality that you want to be executed globally throughout your application. Since you do not have access to the main.js
file in Nuxt like you would in a traditional Vue.js application, you can instead add plugins. These plugins will be initialized before the page is fully rendered and sent to the client. You create a plugin by simply adding a JavaScript file into the plugins
directory. For example, let’s say you want to use Font Awesome, the popular icon font library. We certainly do not want to add the Font Awesome library within each component that needs it. That’s not very efficient or DRY. So let’s fix that with plugins.
Let’s start by creating a new file in the plugins
directory called, font-awesome.js
. Inside of that page, we need to import
a few NPM libraries via ES6’s module syntax. Since plugins do not have access to the main Vue.js library, you need to...