如何在 </body> 结束前在 NUXT 中追加 JS 文件

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

我正在尝试在 NUXT 中附加 javascript 文件。 但是当我使用 nuxt.config 附加 javascript 时它有效但不是我想要的。

 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' }
    ],
  },

当我检查元素时它插入但在头部。

我已经在 google 中进行了搜索,但尚未找到任何解决方案。提前致谢

您有两个选择:

选项 1 - 建议使用指南 Nuxt.js 在文件夹 plugin

中添加 .js

https://nuxtjs.org/guide/plugins

示例:

plugins/example.js

中添加新文件

然后,在 nuxt.config.js 的插件密钥中添加文件:

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

选项 2 - 在 元数据 中与 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>

更多信息:https://github.com/nuxt/nuxt.js/issues/2000#issuecomment-341380762