Jquery VueJs 2不';t在";准备好了吗;功能
这与 我目前正在使用Vue 2.2.4。我创建了一个Vue元素,并在“ready”块中创建了ajax函数,就像上面的示例一样,但没有渲染任何内容。未打印console.log,表明这些ajax请求甚至未被调用。有人知道发生了什么吗?假设我必须使用jQueryAjax库来完成此任务 下面是我的代码:Jquery VueJs 2不';t在";准备好了吗;功能,jquery,ajax,vuejs2,Jquery,Ajax,Vuejs2,这与 我目前正在使用Vue 2.2.4。我创建了一个Vue元素,并在“ready”块中创建了ajax函数,就像上面的示例一样,但没有渲染任何内容。未打印console.log,表明这些ajax请求甚至未被调用。有人知道发生了什么吗?假设我必须使用jQueryAjax库来完成此任务 下面是我的代码: var newJobsVue = new Vue({ el: '#new-jobs', data: { jobs: [] }, methods: {
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
});
}
}
});
var newJobsVue=new Vue({
el:“#新工作”,
数据:{
职位:[]
},
方法:{
就绪:函数(){
var self=这个;
返回$.ajax({
方法:“GET”,
网址:'https://api.indeed.com/ads/apisearch',
跨域:是的,
数据类型:“jsonp”,
数据:{
“v”:“2”,
“格式”:“json”,
“出版商”:,
问:“javascript”,
l:'94112',
userip:'localhost',
用户代理:“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_8_2)”,
拉特朗:“1”
}
})
.完成(功能(响应){
//从搜索\u响应呈现作业
log(“响应为>>>>”,response.results);
//不,这实际上并没有被记录
self.jobs=response.results;
})
.失败(功能(错误){
log(“错误为>>>”,错误);
//这个也没有记录
});
}
}
});
您从未调用过就绪
。试一试
var newJobsVue = new Vue({
el: '#new-jobs',
data: {
jobs: []
},
methods: {
ready: function () {
// alot of code...
}
},
mounted(){
this.ready();
}
});
您还可以使用
created
钩子或运行时间晚于beforeCreate
hook:beforeMount,mounted
的钩子初始化数据在创建的钩子中,您将能够访问方法,反应数据和事件处于活动状态,而在创建之前的钩子中,您无法访问数据和方法
简介:如果您想尽早初始化数据,请使用创建的钩子,或者使用在创建钩子之前运行晚于的钩子来初始化数据
var newJobsVue = new Vue({
el: '#new-jobs',
data: {
jobs: []
},
methods: {
ready: function () {
your ready function
}
},
created(){ // can also be replace with beforeMount and Mounted
this.ready();
}
});
参考:
请注意,ready()
实际上是一个生命周期挂钩,因此您可以使用与mounted()
相同的方法来使用它。他可能只是混淆了方法和生命周期挂钩。@Vue 1中的意思是什么?