Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append JS files in NUXT before </body> ends

Im trying to append javascript files in NUXT. but when i use nuxt.config to append javascript it works but not as I want.

 head: {
    title: 'mynuxt',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: 'Nuxt.js project' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
      { rel: 'stylesheet', href: 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' },
      { rel: 'stylesheet', href: '/css/bootstrap.min.css' },
      { rel: 'stylesheet', href: '/css/mdb.min.css' },
      { rel: 'stylesheet', href: '/css/style.min.css' },
    ],
    script: [
          { src: '/js/bootstrap.min.js' },
          { src: '/js/popper.min.js' },
          { src: '/js/mdb.min.js' }
    ],
  },

when i inspect element it inserted but in head. enter image description here

Ive search already in google but did not found any solution yet. thanks in advance

like image 664
Winston Fale Avatar asked May 02 '18 15:05

Winston Fale


People also ask

Is NUXT JS frontend or backend?

js is a frontend framework built upon Vue. js that offers great development features such as server side rendering, automatically generated routes, improved meta tags managing and SEO improvement.

What is static folder in Nuxt?

The static directory is directly mapped to the server root () and contains files that likely won't be changed. All included files will be automatically served by Nuxt and are accessible through your project root URL.

What is next js vs NUXT JS?

Next. js is basically a React framework that can be used when a user needs fast rendering in complex situations. Nuxt. js on the other hand is a Vue framework that helps in making the web development process efficient and quicker.


1 Answers

You have two options:

Option 1 - With guide Nuxt.js is recommended add .js in folder plugin

https://nuxtjs.org/guide/plugins

Example:

Add new file in plugins/example.js

Then, add the file inside the plugins key of nuxt.config.js:

module.exports = {
  plugins: ['~/plugins/example']
}

Option 2 - Use in metadata with body: true

<script>
export default {
  head: {
    script: [
      { src: '/head.js' },
      // Supported since Nuxt 1.0
      { src: '/body.js', body: true },
      { src: '/defer.js', defer: '' }
    ]
  }
}
</script>

More info: https://github.com/nuxt/nuxt.js/issues/2000#issuecomment-341380762

like image 105
Adriano Resende Avatar answered Sep 27 '22 19:09

Adriano Resende