1

I'm trying to access my data property in my Vue.js component. Looks like I'm missing something obvious.

Here is a short version of my code. StoreFilter.vue is a wrapper for matfish2/vue-tables-2.

<template>
    <store-filter :selected.sync="storeIds"></store-filter>
</template>

<script>
    import StoreFilter from './Filters/StoreFilter';

    export default {
        components: {
            StoreFilter
        },
        data() {
            return {
                options : {
                    requestFunction(data) {

                        console.log(this.storeIds); //undefined

                        return axios.get('/api/orders', {
                            params: data
                        }).catch(function (e) {
                            this.dispatch('error', e);
                        }.bind(this));
                    },
                },
                storeIds: [],
            }
        },
        watch     : {
            storeIds(storeIds) {
                this.refreshTable();
            }
        },
        methods   : {
            refreshTable() {
                this.$refs.table.refresh();
            }
        }
    }
</script>

How to get storeIds from requestFunction?

2
  • 1
    maybe you should put that in methods instead? and if for whatever reason you decide that you need to put it in data use fat arrow function instead i.e. requestFunction: (data) => {} Commented Mar 31, 2018 at 14:03
  • @ A. Lau I can't use methods, cause it's a predefined function, but your solution helped. Please, write it as an answer, so I could accept it. Commented Mar 31, 2018 at 14:24

2 Answers 2

1

Use a closure, see rewrite below.

data() {
  let dataHolder = {};
  dataHolder.storeIds = [];

  dataHolder.options = {
    requestFunction(data) {

        // closure
        console.log(dataHolder.storeIds); // not undefined

        return axios.get('/api/orders', {
            params: data
        }).catch(function (e) {
            this.dispatch('error', e);
        }.bind(this));
    }
  }

  return dataHolder;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I recommend using the created() way to handle this.

export default {
  // whatever you got here
  data () {
    return {
      options: {}
    }
  },
  created () {
    axios.get('/api/orders', { some: params }).then(response => this.options = response.data)
  }
}

5 Comments

It'd probably be better to initialize options: null so you can use v-if to check.
I could also use the Object.keys(options).length > 0 to check. I mean it's really either or here for personal preference. Of course, I am open to find out if null or the Object.keys() method is cleaner and better.
Thank you for your answer, but I can't use methods, cause it's a predefined function.
I am not using method here. I’m using the created () way
@RuChernChong Once again. I can't put this function anywhere, but options. And @A.Lau has already solved this problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.