javascript - Vue js Ready 功能未触发

标签 javascript jquery ajax vue.js

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

这是 vue.js,其中有一个对 Controller 的 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

编辑:更新 vue-resource

正如@EmileBergeron 在评论中指出的那样,vue-resource 本身早在 2016 年 11 月就已停用(几周后我在 vue-resource 的最后一段中提供了这个答案)。以下是有关相同内容的更多信息:

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

关于javascript - Vue js Ready 功能未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209443/

相关文章:

javascript - 删除重复的 ID?

javascript - 创建 JQuery div 类过滤器不起作用

c# - 从 $.post 传递数组到 Action

asp.net - 使用 JavaScript 和 jQuery 获取嵌入 ASP 对象的 ID

c# - 是否可以在 C# 方法中运行 Ajax? (MVC的 Controller )

javascript - AJAX 脚本触发器在 2 个单独的输入字段上触发

javascript - 无法获取AngularJS中下拉菜单的值

javascript - 仅通过网页区分 iPhone 型号?

jquery - 在 jQuery 对象中按 ID 选择以获取文本

javascript - 如何从安全 (HTTPS) 页面访问本地不安全 (HTTP) 服务器