consola icon indicating copy to clipboard operation
consola copied to clipboard

is there a way to require consola throughout nuxt app in order to avoid requiring in each file?

Open tansaku opened this issue 3 years ago • 1 comments

Just wondering if there's a simple way to require consola in, say, nuxt.config.js in such a way that it's available throughout a nuxt project without having to require it in every file?

tansaku avatar Mar 15 '21 17:03 tansaku

You can use a Nuxt plugin and the inject function to inject the consola instance into every Vue context, e.g.


const logger = {{ instantiate consola with options }}
export default ({ app }, inject) => {
  inject('logger', logger)
}

// in components:
export default {
  methods {
    someMethod () {
      this.$logger('mounted')
    }
  },
  asyncData({ app, $logger }) {
    $logger('asyncData')
    // If using Nuxt <= 2.12, use 👇
    app.$hello('asyncData')
  }
}

samtgarson avatar Mar 16 '21 16:03 samtgarson

We might introduce a runtime built-in logger for nuxt but in the meantime, it is better to not use consola directly in runtime plugins. It is not designed exactly for that.

pi0 avatar Apr 10 '23 20:04 pi0