Home > Back-end >  Using Nuxt context inside custom js class
Using Nuxt context inside custom js class

Time:12-07

I'm new in Nuxt, Vue and JS at all. So I can't understand how exactly imports and exports works in JS, what is it about contexts and more..

So I have a class (~/helpers/foo.js)

export default class Foo {
  static bar () {
    console.log(this.$nuxt); // or this.$config, this.$nuxt.$config or smth
  }
}

and in some component I called this function and got (it correctly imported and working) and got "undefined" in console on this console.log

Can someone explain me, how this is working? How can I pass the Nuxt's context to my class (if I can)? Should I define my class as a plugin for Nuxt (and rewrite it to single function 'cos Nuxt's plugins should be a fn? Class in my case used as a namespace for some functions, that is used for ab-testing)

CodePudding user response:

First off, if you're new to JS, you should not be using Vue. Even less Nuxt, learn the basics and come back. You'll get a far easier time debugging and learning overall.
Skipping steps will not good in the mid/long term.

As for your question, you don't need to use Classes overall, especially for what you showed.

You can use the following answer to have both a successful export import and the usage of Vue/Nuxt's context in a .js file. Reading the export page from MDN can also help quite a lot.

  • Related