8

In vue 2 you can access the Vue instance Global api from .js files like this:

Vue.prototype.$auth

in Vue 3 you have the app instance, which as far as I know right now only exists within main.js

so if for instance, I have helper.js, how do I access app.config.globalProperties.$auth from the help file?

1 Answer 1

10

You could define a plugin :

// plugins/auth.js
export default {
  install: (app, options) => {
    app.config.globalProperties.$auth={}
  }
}

then use it in main.js

import authPlugin from './plugins/auth'

app.use(authPlugin)

Or try to export the app instance from the main.js and use it in your helper.js file :

export app;

helpers.js

import {app} from './main'

or you could pass that global variable when you call the helper function inside any component.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the export app from main.js tip. In the helper function, after importing app from main, access the global properties using app.config.globalProperties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.