Mojtaba Barari
Mojtaba Barari

Reputation: 1274

How create an utils in Nuxtjs

I'm using Nuxtjs 2.15.4 and I wanna revise my Utils. Right now I'm using a mixin as my utils but I wanna do something like this:

import {func1 , func3} from '~/plugins/mixins/utils.js'
export default{
  // ------------
  methods:{
    myMethod(){
      this.func1()
    },
  }
}

I also want my utils be like a mixin so I can have access to this. ; because I use this to access store or $router or other methods and computed data in my utils.js .

Is it possible at all?

Upvotes: 4

Views: 3505

Answers (1)

You could create a plugin and include this in the nuxt.config.js file where you 'inject' objects or methods. Now you can also access the (store) context.

export default ({ app }, inject) => {
  /* Here we inject the method 'func1' */
  inject('func1', (params) => console.log('This is func1', params))
}

and then you can use it like this

methods: {
    myMethod() {
      this.$func1()
    }
}

Here you can read more about inject

Upvotes: 3

Related Questions