0

I have a global variable that is populated with an API call when a component is mounted. I also have a chart component that I would like to show if that variable is not null (i.e. has the request has finished and it has been populated).

At the moment to render the chart I am using this:

<template>
  <div class="container">

    <b-chart
        v-if="$store.state.lists[api_key] != null"
        :chartdata="$store.state.lists[api_key]"
        :options="options"
    />
  </div>
</template>

I have tried moving this check $store.state.lists[api_key] != null to computed or watch, to minimise the inline scripting, but I can't seem to get it to work. Would someone please show me how.

2 Answers 2

1

Try this:

computed: {
    canShowChart() {
        return this.$store.state.lists[this.api_key] != null;
    }
}
<b-chart
    v-if="canShowChart"
    :chartdata="$store.state.lists[api_key]"
    :options="options"
    />
Sign up to request clarification or add additional context in comments.

4 Comments

I could have sworn, I tried this and it did not work. But I have just tried again and it works fine. Thanks
if i wanted to call a function if canShowChart becomes true. How would I go about that?
@matt your probably right, i made some edits because i made a mistake when i first posted
you could watch the computed value canShowChart stackoverflow.com/questions/41067378/…
1

Since null values are interpreted as "falsy", and assuming you have an "api_key" data variable, you can use it this way:

computed: {
    chartData() {
        return this.$store.state.lists[this.api_key]
    }
}
<template>
  <div class="container">
    <b-chart
        v-if="chartData"
        :chartdata="chartData"
        :options="options"
    />
  </div>
</template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.