10

My vue component is like this :

<template>
    <div>
        <div class="panel-group"v-for="item in list">
            <div class="col-md-8">
                <div class="alert">
                    {{ status = item.received_at ? item.received_at : item.rejected_at }}
                    <p v-if="status">
                        {{ status }} - {{ item.received_at ? 'Done' : 'Cancel' }}
                    </p>
                    <p v-else>
                        Proses
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        ...
        computed: {
            list: function() {
                return this.$store.state.transaction.list
            },
            ...
        }
    }
</script>

I want define the status variable

So, the status variable can be used in condition

I try like this :

{{ status = item.received_at ? item.received_at : item.rejected_at }}

But, seems it was wrong

How I define it correctly?

1
  • why don't you use a getter? Commented Apr 4, 2017 at 13:00

2 Answers 2

3

You can use a method to have the functionality as the item variable would not be present outside the scope of the v-for

 ...
 <div class="alert">
   <p v-if="status(item)">
      {{ status(item) }} - {{ item.received_at ? 'Done' : 'Cancel' }}
   </p>
   <p v-else>
      Proses
   </p>
 </div> 
 ...

and in the component:

methods: {
  ...
  status (item) {
    return (item) 
             ? (item.received_at) ? item.received_at : item.rejected_at
             : false
  }
  ...
}
Sign up to request clarification or add additional context in comments.

10 Comments

item.received_at taken from the loop <div class="panel-group"v-for="item in list">. It is not readable in the computed
item must be a data property?
data() { return { item: this.$store.state.transaction.list } },. Like that?
Yes, I've seen it. But seems it remains to be defined in the data ()
@samueltoh does adding the computed status() not help?
|
3

You need to use data :

export default {
    ...
    data: function() {
        return {
            status: false
        }
    },
    computed: {
        list: function() {
            return this.$store.state.transaction.list
        },
        ...
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.