Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue + nuxt js - how to access context in a plugin for server side?

I like to know if there is a way to access in a plugin the context variables? Which other variables / objects exists apart from the "process" object? Unfortunately I could not found a clear description nor a reference about the "process" object at the nuxt side

i.e. plugins/helper.js

const myHelper = {
  helpFunction(arg) {
    if (process.server) {
      ...
    }
    return ...;
  },
like image 230
megloff Avatar asked Dec 21 '25 03:12

megloff


1 Answers

If you export a function in your plugin, you will automatically have access to nuxt context as it's first argument (docs):

    export default (context, inject) => {
      context.app.helpFunction= (arg) => {
          if (process.server) {
             ...
          }
          return ...;
      }
    }

now you can use your helper function in any component like this:

    this.$helpFunction(someArgs)...
like image 79
Ahmad Mobaraki Avatar answered Dec 24 '25 03:12

Ahmad Mobaraki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!