javascript - Interaction with jQuery's ajax background in vuejs
高洛峰
高洛峰 2017-05-19 10:41:03
0
3
532

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
仅有的幸福
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梦

Then you write it outside, why write it inside the showData method?

漂亮男人

You can read the vue documentation about the component life cycle. Mounted() or created() can be used to meet your needs

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template