Initializing Vue data with AJAX
I was able to solve my problem by performing my necessary action within the success handler on the AJAX call. You can either put the entire Vue object creation in there, or just set the data you need.
You can make the ajax call inside of the mounted function (“ready” in Vuejs 1.x).
<script type="text/javascript">
var ItemsVue = new Vue({
el: '#Itemlist',
data: {
items: []
},
mounted: function () {
var self = this;
$.ajax({
url: '/items',
method: 'GET',
success: function (data) {
self.items = JSON.parse(data);
},
error: function (error) {
console.log(error);
}
});
}
});
</script>
<div id="Itemlist">
<table class="table">
<tr>
<th>Item</th>
<th>Year</th>
</tr>
<tr v-for="item in items">
<td>{{item.DisplayName}}</td>
<td>{{item.Year}}</td>
</tr>
</table>
</div>
I had same problem, fixed by Samuel De Backer's answer above.
The problem is at ajax success callback function,
if you use this.data, it is incorrect, because when 'this' reference the vue-app, you could use this.data, but here (ajax success callback function), this does not reference to vue-app, instead 'this' reference to whatever who called this function(ajax call).
So you have to set var self = this before ajax, then pass into callback function (success call back)
Here is my working code
created () {
this.initialize()
},
mounted () {
this.getData()
},
methods: {
getData() {
var getUser_url = url + 'cfc/sw.cfc?method=getUser&returnformat=json&queryformat=struct';
console.log(getUser_url )
/*
You can use a plethora of options for doing Ajax calls such as Axios, vue-resource or better yet the browser's built in fetch API in modern browsers.
You can also use jQuery via $.ajax() API, which simply wraps the XHR object in a simple to use method call
but it's not recommended to include the whole jQuery library for the sake of using one method.
http://updates.html5rocks.com/2015/03/introduction-to-fetch
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses.
It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.
*/
// ********** must use self = this **************
// this reference vue-app. must pass it to self, then pass into callback function (success call back)
var self = this;
fetch(getUser_url).then(function (response) {
return response.json();
}).then(function (result) {
console.log(result);
// must use self.user, do not use this.user,
// because here, this's scope is just the function (result).
// we need this reference to vue-app,
self.user = result; // [{}, {}, {}]
}); // fetch(){}
console.log(this.user);
},
initialize () {}