简体   繁体   中英

Nuxt js How to import js file from assets?

I tried all the ways to import the JS file but still not found. how to import js file from assets to nuxt.config.js and to all your website

nuxt.config.js

head: {
script: [
      {
        src: '~/assets/js/core.js',
      },
      {
        src: 'js/core.js',
      },
      {
        src: 'assets/js/core.js',
      },
      {
        src: '~assets/js/core.js',
      },
      {
        src: '@assets/js/core.js',
      },
      {
        src: '@/assets/js/core.js',
      },
      {
        src: '@assets/js/core.js',
      },
]
}

You can use plugins directory to add js plugins to your Nuxt project.

Example - hello.js

export default ({ app }, inject) => {
 // Inject $hello(msg) in Vue, context and store.
 inject('hello', msg => console.log(`Hello ${msg}!`))
}

nuxt.config.js

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

component.vue

export default {
  mounted() {
    this.$hello('mounted')
    // will console.log 'Hello mounted!'
  }
}

Example reference - https://nuxtjs.org/docs/directory-structure/plugins/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM