xeraphim
xeraphim

Reputation: 4645

Using a config file in Vue.use

In my index.ts file I'm configuring my google recaptcha component with the sitekey:

Vue.use(VueReCaptcha, {
    siteKey: 'my_redacted_sitekey'
});

this happens before the new Vue({ ... }) statement.

I don't really want the siteKey hardcoded in the index.ts because we would have to redeploy it it would change.

Is there a way to outsource this value to a config file and use it here or is this not possible before the new Vue({}) statement?

Thanks in advance

Upvotes: 0

Views: 108

Answers (1)

Dan
Dan

Reputation: 63099

You could place a config file in the /public folder and fetch it. (If you put it in /src it will be compiled.)

Simply fetch the config from any module including main.js at any time:

axios.get('config.json').then(response => {
  const json = response.data;
  // do something with the config
})

Ex. using axios

Remember to be mindful of site security as it's in the public folder this way.

There might be a temptation to load the Vue app inside the callback too, but this would cause a blank page until the file is loaded. It would be better to start the app with a loading message than to delay it with a blank page, for several reasons.

Upvotes: 1

Related Questions