Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store static content (e.g. country codes) in a vue.js app?

Tags:

vue.js

vuex

I have tried to find the common best practice for this issue but I seem to be looking in the wrong places. What's the best practice to store content such as country codes (or any other static content, e.g. an array of categories etc.) in a vue.js app? It would be awkward to save that in my .env file as an environment variable and it isn't really another config variable either.

Should I just store it in Vuex even though this is immutable data and won't be changed by the user or app ever? Or should I just create my own js file and import it whereever I need it? In AngularJS I just put it in a HelperService as a function and that was it...

function getCountryArray() {
        var countries = {
              'AF': 'Afghanistan',
              'AX': 'Åland Islands',
              'AL': 'Albania',
              'DZ': 'Algeria',
              'AS': 'American Samoa',
              'AD': 'Andorra',
              'AO': 'Angola',
              'AI': 'Anguilla',
              'AQ': 'Antarctica',
              'AG': 'Antigua and Barbuda',
              'AR': 'Argentina'
...
like image 233
axelwittmann Avatar asked May 29 '18 15:05

axelwittmann


People also ask

Is VueJS static?

js is a higher-level framework built on top of the Vue ecosystem which provides an extremely streamlined development experience for writing universal Vue applications. Better yet, you can even use it as a static site generator (with pages authored as single-file Vue components)!

How do I create a store in Vue JavaScript?

// store. js import Vue from 'vue'; import Vuex from 'vuex'; Vue. use(Vuex); const store = new Vuex. Store({ state: { count: 0 }, mutations: { increment (state, payload){ return state.

What is public folder in VueJS?

The Vue CLI has generated the files and folders needed to get started with our Vue. js development. Let's look at each of these in turn: node_modules folder : This folder contains the libraries downloaded from the npm. public folder : This folder contains the HTML file and favicon.


1 Answers

You can make any property / function accessible on every vue instance (component) using Vue.prototype.

Vue.prototype.getCountries = {
  'AF': 'Afghanistan',
  'AX': 'Åland Islands',
  'AL': 'Albania',
  'DZ': 'Algeria',
  'AS': 'American Samoa'
};
 
Vue.component('test', {
  template: `<div>{{ getCountries }}</div>`,
  created() {
    console.log('countries ->', this.getCountries);
  }
})
 
new Vue({
  el: '#app',
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>

<div id="app">
  <test />
</div>

Another alternative would be defining a global mixin and will work the same way.

Vue.mixin({
  data() {
    return {
      get getCountries () {
        return {
         'AF': 'Afghanistan',
         'AX': 'Åland Islands',
         'AL': 'Albania',
         'DZ': 'Algeria',
         'AS': 'American Samoa'
      };
    }
  }
})

Personal Preference

I prefer to have it on a .js (notice you also can import mixins to only use it in specific components - Vue Mixins) file and import it only in the components that will be used, to not overload all components with values not required.

like image 95
DobleL Avatar answered Sep 19 '22 20:09

DobleL