Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Nuxt JS use injected function from plugin in another plugin file

I'm using Nuxt JS's inject function to inject a function into my page for code reusability. I'd like to use the function in another plugin file and can't seem to get the function in there.

Here's my set up:

  • plugins/utils/tracking.js
function setCookiePrefix () {
  return 'fudge__'
}

function getCookie (app, name) {
  try {
    const prefix = setCookiePrefix()
    const cookie = app.$cookies.get(`${prefix}${name}`)

    if (!cookie || cookie == '') {
      throw 'cookie not set'
    }

    return cookie
  } catch (err) { }

  return null
}

export default function ({ app, store, context }, inject) {

  /*
  * Get just the affiliate (cpm_id OR affiliate)
  * examples: "my_brand", "blah"
  */
  inject('getAffiliate', () => {
    const affiliate = getCookie(app, 'affiliate')
    const brand = getBrand(store)

    if (!affiliate) return brand

    return affiliate
  })

}

And the file I'm trying to utilise the getAffiliate function in from my tracking.js file:

  • plugins/init-brand.js
export default async function ({ app, route, store }) {
  const affiliate = app.$getAffiliate()
  console.log(affiliate) <-- undefined
}

I've tried:

  • app.$getAffiliate()
  • this.$getAffiliate() <-- this works in a Vue file
  • $getAffiliate()
  • this.getAffiliate()

What am I missing to access my getAffiliate function in another plugin file?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have an api plugin with some axios set up. I can load it inside another plugin file like this:

export default ( {$api} , inject) => {
  const service = new SomeService($api)
  inject('someService', SomeService)
}

So in your case that would be:

export default async function ({ app, route, store, $getAffiliate }) {
  const affiliate = $getAffiliate()
  console.log(affiliate)
}

Not sure if this is the right way, but it works for me.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!