Vue js Ready函数没有被触发
Vue js Ready function is not triggered
我有这个值函数,基本上有两个方法。第一个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(){}
。
- document.ready是在async js之后加载的
- 如何在One'中包含jQuery.js;s Own.js在$(document).ready之前
- $(document).ready() 在 IE 中使用 require.js 优化代码
- 当 document.ready 中的函数完成时执行 js 函数
- 如何在 html 中调用 js document.ready 函数
- 如何调用 Document.ready 内部的外部.js,方法是从 HTML 传入变量来调用下面的.js文件
- JS:创建函数并在$(document).ready中声明的正确方法
- 删除 DOM ready 上的凌乱图像标签并使用 JS 正确设置
- 将JS分解为带有$(document).ready()的文件,但保留范围
- document.ready在外部JS文件中,用于检测加载哪个文件
- document.ready和angular js中的加载函数
- $(document).ready 函数之外的 JS 全局变量和函数
- document.ready用angular.js以无限循环调用
- 如何让这个特定的animatedModal.js加载到document.ready
- Vue js Ready函数没有被触发
- jQuery ready()是否在所有JS文件加载/执行后触发
- 一个包含jquery的解决方案;Js在HTML的末尾,而同时,包含在中间的控件需要$(document).ready
- 如何在document.ready上调用多个js函数而不将它们放入jQuery(document).ready(func
- 将jquery和jquery(document).ready(function(){组合在一个js文件中
- 在多个js文件中具有多个$(document).ready函数的含义