I want to use vuejs to call jQuery's ajax, but the event on the page is triggered. I want it to be triggered as soon as the page is loaded. How to do it?
The code is as follows:
HTML:
<p id="app">
<button id="but" v-on:click="showData">测试加载数据</button>
<table border="1">
<tr v-for="data in users">
<td>{{data.name}}</td>
<td>{{data.details1}}</td>
<td>{{data.details2}}</td>
</tr>
</table>
</p>
js:
var vum = new Vue({
el:"#app",
data:{
users:""
},
methods:{
showData:function(){
$.ajax({
url:"请求路径",
type:"post",
dataType:"json",
success:function(result){
if(result.state==0){
vum.users = result.data;
}
},
error:function(){
alert("请求失败");
}
});
}
}
});
What should I do to send an ajax request as soon as the page is loaded? Please give me some advice, thank you very much
仅有的幸福2017-05-19 10:43:03
var vum = new Vue({
el:"#app",
data:{
users:""
},
created () {
$.ajax({
url:"请求路径",
type:"post",
dataType:"json",
success:function(result){
if(result.state==0){
vum.users = result.data;
}
},
error:function(){
alert("请求失败");
}
});
},
methods:{
}
});
过去多啦不再A梦2017-05-19 10:43:03
Then you write it outside, why write it inside the showData method?
漂亮男人2017-05-19 10:43:03
You can read the vue documentation about the component life cycle. Mounted() or created() can be used to meet your needs