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

353
Visualizações
Why can't I use .includes on a string in v-if when using a computed property?

Issue

I am trying to determine whether or not a string includes some substring in Vues v-if directive. When I do, I get the following error:

TypeError: $options.language.includes is not a function

I am using Vue, Vuex and Ionic.

Where language is a computed property:

    computed: {
        language() {
            return this.$store.getters.getLanguage;
        }
    }

And the v-if looks as such:

<ion-label v-if="language.includes('en')">Play as guest</ion-label>

Here is what the computed property looks like in the store:

    state: {
        language: String,
    }

Mutations:

    mutations: {
        setLanguage(state, payload) {
            state.language = payload;
        }
    }

Actions:

actions: {
        setLanguage(state, language) {
            state.commit('setLanguage', language);
        }
}

Getter:

    getters: {
        getLanguage: state => state.language,
    }

And the language is set in the Ionic hook ionViewWillEnter:

    async ionViewWillEnter() {
        const info = await Device.getLanguageCode();
        this.$store.dispatch('setLanguage', info.value);
    }

Which fires when the component routing to is about to animate into view

Attempts at solution

  1. I have made sure the computed property is of type string using typeof
<ion-label v-if="typeof language === 'string'">Play as guest</ion-label>

Which displays the label meaning the computed property is a string

  1. I have tried using indexOf
language.indexOf('en') > -1

But this results in:

TypeError: $options.language.indexOf is not a function

And now I'm not quite sure how to proceed. I need to be able to check whether or not the computed property string includes some substring in the v-if directive. How can I achieve this? Thank you for your help!

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

0

The explanation for the error is that language is defined (otherwise the error would be about missing includes property) but doesn't have includes method. This means that it's an object or a function.

That it typeof language === 'string' condition is true at some point means that a value changes from a non-string to a string.

The problem is:

    state: {
        language: String,
    }

Unless this code belongs to component props that use special logic to check prop types and accepts constructors for primitive types, this means that state.language is assigned with String constructor, i.e. a function.

It should be:

language: null,

or

language: '',

or

language: 'en',

Notice that uninitialized value may need a special treatment, null is preferable because it designates that a value is uninitialized and results in clear unambiguous errors. In this case it would be:

Cannot read property 'includes' of null

It's a bad practice to put logic into templates. This makes it harder to debug and handle values, also may cause performance issues for intensive calculations. This is the use case for computed properties:

computed: {
    language() {
        return this.$store.getters.getLanguage;
    },
    isDefaultLanguage() {
        return !this.language || this.language.includes('en');
    }
}
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