skip to Main Content

I am a newbie in Vue.js. Not sure how to return a function result to data value.

I have an array of objects. Each has data and a query expression.

I have two functions: First one changes request expression (string) and turns it into valid json then calls second function (where I pass data of an object) Second – sends an API request and in case of valid response writes it into passed data.

I want to use function dynamically (I mean I’ve got three requests but I want to use single function that only changes the request).

The code:

new Vue({
    data: {
        dropList: [
            {
                label: "label_1",
                data: [],
                expresion: "expression_1"
            },
            {
                label: "label_2",
                data: [],
                expresion: "expression_2"
            }
        ],
        chartData: []
    },
    
    methods: {
        getDropListData() {
            
            this.dropList.forEach( value => {
                var config = {
                    expression: value.expresion,
                    applyDimensionRights: true,
                };
                this.getData(JSON.stringify(config), value.data);
            });
        },
        
        datasourceExecute() {

            var config = {
                expression: "expression_3",
                applyDimensionRights: true,
            };
            
            this.getData(JSON.stringify(config), this.chartData);
    
        },
        
        getData(payload, targetDataSetName) {
            var url = "url";
            this.sendRequest(
                url,
                "POST",
                payload,
                function (responseData) {
                  if (responseData.isOK) {

                    targetDataSetName = responseData.data;

                  } else {
                    this.makeToast(responseData.message, "danger");
                    console.log("Error getting data");
                  }
                }.bind(this)
            );  
        },

responseData is OK. I get a valid response in the console. However data isn’t changing.

2

Answers


  1. Chosen as BEST ANSWER

    I figuered it out: I passed a reference not a value therefore no changes for initial data were made


  2. You need to declare data option as a function with a return value, not an
    object:

    new Vue({
      data(){
        return {
          ...
        }
      }
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search