How to add external script tag in head in Nuxt

featured Image

In this article we will learn how to add external javascript scripts in the head() in Nuxt components.

Usually when we have to add some scripts in our nuxt application we add them globally in our nuxt.config.js file.

The nuxt.config.js file let us import script tags and other style tags which is applied in all our pages.

But if we want to run a script only in one component then we have to add it only in that particular component. NuxtJS has made it easy to do it by its head() method.

Nuxt uses vue-meta which helps to update the headers and html attributes of our application. Using the head() method provided by it we can add any JS script to our current page.

Add External script links in Nuxt components.

In Nuxt you can use head as a function or as an Object. By using it as a function, it will provide us access to data and computed property.

So to add external JS script locally in our application:

 export default {
    head() {
      return {
        script: [
          {
            src:
              'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'
          }
        ],
      }
    }
  }

That’s how you can add external scripts in your component / page. If you want to add multiple scripts just add it inside the array.

Related Topics:

Add External Script Tags in Vue JS component – Solutions

Related Posts

featured Image

How To Add Robots.txt in your Nuxtjs Application

Here in this article, we will see how to add a robot.txt file in our nuxtjs application after it is built or generated before deployment. Before learning the steps to…

Read more
featured Image

How To Add Font Locally In Nuxt App

In this tutorial, we will learn the step-by-step process of how to add a custom google font locally in your nuxtjs application. You can either use a premium font or…

Read more
featured Image

How To Add Google Font in Nuxt Application

Here in this article we will see how to add google font in your nuxtjs project/application. The process is really simple you just have to import the google font link…

Read more