1

My vue component like this :

<template>
    ...
        <ul v-if="!selected && keyword">
            <li v-for="state in filteredStates" @click="select(state.name)">{{ state.name }}</li>
        </ul>
    ...
</template>
<script>
    export default {
        ...
        computed: {
            filteredStates() {
                const data = this.$store.dispatch('getProducts', {
                    q: this.keyword
                })
                data.then((response) => {
                    console.log(response.data)
                    return response.data
                })
            }
        }
    }
</script>

The result of console.log(response.data) like this :

enter image description here

I want to display array data like the image above. But it is not show the value. Maybe my loop in the vue component is still wrong

How can I solve this problem?

1 Answer 1

2

filteredStates performs an async API request, you mustn't do this in a computed property (in fact, you're not returning anything from that property anyway so it's useless).

You should make filteredStates a data property, then watch for changes to keyword then update filteredStates in response.

Something like:

data() {
  return {
    filteredStates: []
  }
},

watch: {
  keyword(value) {
    this.$store.dispatch('getProducts', { q: value })
    .then(res => {
      this.filteredStates = res.data;        
    })
  }
}
Sign up to request clarification or add additional context in comments.

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.