Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!