Vue js Ready函数没有被触发

Vue js Ready function is not triggered

本文关键字:js Ready 函数 Vue      更新时间:2023-09-26

我有这个值函数,基本上有两个方法。第一个postStatus用于在用户点击保存按钮后保存帖子,另一个getPosts用于从数据库中检索该用户以前的所有帖子。

这里是vue.js,这里有一个ajax调用控制器(在Laravel 5.3)

$(document).ready(function () {
    var csrf_token = $('meta[name="csrf-token"]').attr('content');
    /*Event handling within vue*/
    //when we actually submit the form, we want to catch the action
    new Vue({
        el      : '#timeline',
        data    :   {
            post    : '',
            posts   : [],
            token   : csrf_token,
            limit   : 20,
        },
        methods : {
            postStatus : function (e) {
                e.preventDefault();
                //console.log('Posted: '+this.post+ '. Token: '+this.token);
                var request = $.ajax({
                    url         :   '/posts',
                    method      :   "POST",
                    dataType    :   'json',
                    data        :   {
                        'body'  :   this.post,
                        '_token':   this.token,
                    }
                }).done(function (data) {
                    //console.log('Data saved successfully. Response: '+data);
                    this.post = '';
                    this.posts.unshift(data); //Push it to the top of the array and pass the data that we get back
                }.bind(this));/*http://stackoverflow.com/a/26479602/1883256  and  http://stackoverflow.com/a/39945594/1883256 */
                /*request.done(function( msg ) {
                    console.log('The tweet has been saved: '+msg+'. Outside ...');
                    //$( "#log" ).html( msg );
                });*/
                request.fail(function( jqXHR, textStatus ) {
                    console.log( "Request failed: " + textStatus );
                });
            },
            getPosts : function () {
                //Ajax request to retrieve all posts
                $.ajax({
                    url         :   '/posts',
                    method      :   "GET",
                    dataType    :   'json',
                    data        :   {
                        limit   :   this.limit,
                    }
                }).done(function (data) {
                    this.posts = data.posts;
                }.bind(this));
            }
        },
        //the following will be run when everything is booted up
        ready : function () {
            console.log('Attempting to get the previous posts ...');
            this.getPosts();
        }
    });
});

到目前为止,第一种方法postStatus工作良好。

第二个应该在ready函数中调用或触发,然而,什么也没有发生。我甚至没有得到console.log消息Attempting to get the previous posts ...。好像从来没开过枪。

有什么问题吗?我怎么修理它?

注意:我使用jQuery 3.1.1, Vue.js 2.0.1

我看到您正在使用Vue 2.0.1。Vue 2.0及以上版本没有ready方法

这里是所有Vue 2.0更改列表的链接:https://github.com/vuejs/vue/issues/2873

如上文所述,您可以使用mounted而不是ready

不是一个问题,但只是一个注意:你正在广泛地混合jQuery和Vue。如果您只需要jQuery用于http相关功能,您可以使用vue-resource - https://github.com/vuejs/vue-resource

EDIT: Update on vue-resource

正如@EmileBergeron在评论中指出的那样,vue-resource早在2016年11月就已经退休了(就在我在vue-resource的最后一段提供这个答案的几周之后)。这里有更多相同的信息:

https://medium.com/the vue - point/retiring vue -资源- 871 a82880af4

@Mani的建议使用mounted(){}作品的组件,不是隐藏的。如果您希望在组件中运行一个可见的函数,并且使用v-if=""v-show=""等条件隐藏元素,则使用updated(){}