javascript - got property undefined when fetching data in ajax using vue.js -


i have got undefined when alert param fetching ajax using vue.js, here code.

test.json return:

[   {isactive: false,name: test} ] 

js:

new vue({ el: '#viewport', data: {      test_data: [] }, mounted: function () {     this.fetchtestdata(); }, methods: {     fetchtestdata: function () {         $.get(test.json, function (data) {              this.test_data = data;             alert(this.test_data.isactive);         });      } } }); 

i beginner of vue.js, hope have reply, thanks.

if fetching data test.json file, first need because that's not validate json:

[   {     "isactive": false,     "name": "test"     } ] 

and need use bind because this not referring vue instance

    fetchtestdata: function () {         $.get('test.json', function (data) {             this.test_data = data;             alert(this.test_data[0].isactive);         }.bind(this));     } 

and accessing data this.test_data[0].isactive because it's array


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -