capiono
capiono

Reputation: 2997

access vue 3 app instance api from non-vue fiile

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?

Upvotes: 8

Views: 6609

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

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.

Upvotes: 10

Related Questions