調用Ajax請求這是我目前使用的Vue 2.2.4有關Initializing Vue data with AJAXVueJs 2不會在「準備就緒」功能
。我在上面的例子中創建了一個Vue元素和「ready」塊內的ajax函數,但沒有渲染。沒有打印console.log表明這些ajax請求甚至沒有被調用。任何人都知道發生了什麼事?假設我必須爲此任務使用jQuery ajax庫。
下面是我的代碼:
var newJobsVue = new Vue({
el: '#new-jobs',
data: {
jobs: []
},
methods: {
ready: function() {
var self = this;
return $.ajax({
method: 'GET',
url: 'https://api.indeed.com/ads/apisearch',
crossDomain: true,
dataType: 'jsonp',
data: {
'v': '2',
'format': 'json',
'publisher': <My_Key>,
q: 'javascript',
l: '94112',
userip: 'localhost',
useragent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)',
latlong: '1'
}
})
.done(function(response){
//render the jobs from the search_response
console.log("response is>>>>>", response.results);
//nope, this is not actually logged
self.jobs = response.results;
})
.fail(function(error){
console.log("error is>>>", error);
// neither is this one logged
});
}
}
});
注意'ready()'實際上是一個生命週期鉤子,所以你可以像掛載()一樣使用它。他可能只是混淆了方法和生命週期鉤子。 –
@woutr_be你的意思是在Vue 1? – Bert
Vue 2.2.4應該支持https://vuejs.org/v2/api/#created –