FeelRightz
FeelRightz

Reputation: 2999

Set global variables from another js file in vue 3

Folder structure

main.js
/test/index.js

main.js file

import { createApp } from 'vue'
const app = createApp(App);
//the rest of code
app.config.globalProperties.$apple = "apple value";
app.config.globalProperties.$orange = "orangevalue";

/test/index.js file

app.config.globalProperties.$testvalue = "123";

How to set the global variable in /test/index.js in vue3

Upvotes: 0

Views: 686

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

In /test/index.js define a plugin with this content :

export default {
  install: (app) => {
    app.config.globalProperties.$testvalue = "123";
  }
}

then in main.js use it in this way app.use(testPlugin):

import { createApp } from 'vue'
import testPlugin from './test'

const app = createApp(App);
//the rest of code
app.config.globalProperties.$apple = "apple value";
app.config.globalProperties.$orange = "orangevalue";
app.use(testPlugin)

Upvotes: 1

Related Questions