Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
How to use variable globally in vuejs

So I have a vuejs project and want to use some variable (globally) in any of my components (Variable will not change its value). I created a variable called STEPS in somefile.js and imported it from components where I want to use.

// somefile.js

export const STEPS = {
  PROJECT: 'project',
  USER: 'user',
  SUMMARY: 'summary',
}

// maybe freeze object here because value will not be changed
// component1.vue

import { STEPS } from 'somefile.js'

export default {
   methods: {
     someMethod(value) {
       if (value === STEPS.PROJECT) {
         // do something
       }
     }
   }
}

// component2.vue

import { STEPS } from 'somefile.js'

export default {
   methods: {
     someMethod(value) {
       if (value === STEPS.USER) {
         // do something
       }
     }
   }
}

So this actually works? I don't get any errors or anything. But I'm just wondering .. I am not sure if it's okay to use it like this? Just making a .js file and import it anywhere in your code and use it?

So.. I found 2 ways how people use global variables in vuejs.

  1. using instance properties https://vuejs.org/v2/cookbook/adding-instance-properties.html#Base-Example
Vue.prototype.$STEPS = {
  PROJECT: 'project',
  USER: 'user',
  SUMMARY: 'summary',
}


created() {
   console.log(this.$STEPS)
}

  1. Using mixins https://vuejs.org/v2/guide/mixins.html
var mixin = {
  data: function () {
    return {
      message: 'hello',
      foo: 'abc'
    }
  }
}

new Vue({
  mixins: [mixin],
  data: function () {
    return {
      message: 'goodbye',
      bar: 'def'
    }
  },
  created: function () {
    console.log(this.$data)
    // => { message: "goodbye", foo: "abc", bar: "def" }
  }
})

So my question is is it okay to use how I used global variable? Just making a variable in javascript file and importing it.. Or should I change it to using instance properties or mixins?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, the way you do it is totally fine. If you want to manage your data centrally where is can be accessed by all components (and can even be changed across all of them simultaneously), you could also have a look at Vuex. Here is also a great (but maybe outdated) tutorial on Vuex.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda