我使用这段代码学习vuejs,并使用WordPress REST API制作一个公文包无限滚动。我可以呈现前十篇文章,但在滚动时,vue将停止工作,并出现与api提供的特色图像url相关的错误:
[Vue warn]:呈现错误:“TypeError:无法读取未定义的属性“wp:featuredmedia”(在根目录中找到)
引用错误的代码行是self.posts.push(data);
TypeError:无法读取未定义的属性“wp:featuredmedia”
这一行表示另一个错误:
$.getJSON( \'wp-json/wp/v2/posts?categories=3&page=\'+page+\'&per_page=10&_embed\', function(data){}
代码如下:
<?php get_header(); ?>
<div class="container" id="app">
<div class="row" style="padding:2em;margin:2em 0 0 0;" v-on:scroll="loadItems()">
<div class="card-columns">
<div class="card" v-for="post in posts">
<img class="card-img-top" v-bind:src="post._embedded[\'wp:featuredmedia\'][0].source_url" />
<h4 class="" v-html="post.title.rendered"></h4>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
(function($){
$(document).ready(function(){
var page = 1;
var canBeLoaded = true;
var app = new Vue({
el: \'#app\',
data: {
posts: []
},
created: function() {
window.addEventListener(\'scroll\',this.loadItems);
},
destroyed: function() {
window.removeEventListener(\'scroll\', this.loadItems);
},
mounted: function(){
console.log(\'mounted fired\');
var self = this;
$.getJSON( \'wp-json/wp/v2/posts?categories=3&page=1&per_page=10&_embed\', function(data){
console.log(data);
self.posts = data;
});
},
methods: {
loadItems: function(){
var bottomOffset = 2000;
var self = this;
if( $(document).scrollTop() >= ( $(document).height() - $(window).height() - 10 ) && canBeLoaded == true ){
console.log(\'loadItems method fired\');
console.log(page);
canBeLoaded = false;
$.getJSON( \'wp-json/wp/v2/posts?categories=3&page=\'+page+\'&per_page=10&_embed\', function(data){
if( data.length > 0 ){
self.posts.push(data);
page++;
}
}).done(function(){
canBeLoaded = true;
});
}
}, // end loadItems
} // end methods
});
// for debug and prototype only - remove in production
// var root = $(\'html, body\');
// $(\'#scroll-link\').on(\'click\', function(e){
// e.preventDefault();
// root.animate({
// scrollTop: $( $.attr(this, \'href\') ).offset().top
// }, 500);
// return false;
// });
//
}); // doc ready
}(jQuery));
</script>
我确信图像已返回,但为什么会出现此错误?
SO网友:sialfa
在阅读了vue在更新数据时如何工作之后,我找到了一个解决方案。我的问题是Array.push()
函数将新获取的数据作为嵌套数组添加到原始数组中。这导致了v-for
无法在嵌套数组上迭代的循环。有人建议我使用spread操作符,但这个解决方案正确地更新了原始数据,但同时以前的帖子被新数据所取代,这不是我所期望的无限滚动加载行为。我现在的代码运行良好,我发现并测试的最佳方法是使用服务器端渲染生成初始帖子,然后在滚动时使用vue加载新项目。在代码中,我使用加载
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
(function($){
$(document).ready(function(){
var page = 1;
var canBeLoaded = true;
var app = new Vue({
el: \'#app\',
data: {
posts: []
},
created: function() {
window.addEventListener(\'scroll\',this.loadItems);
},
destroyed: function() {
window.removeEventListener(\'scroll\', this.loadItems);
},
mounted: function(){
console.log(\'mounted fired\');
// this part works fine, but I\'ve opted for a server-side rendering for the initial contents
var self = this;
$.getJSON( \'wp-json/wp/v2/posts?categories=3&page=1&per_page=10&_embed\', function(data){
console.log(data);
self.posts = data;
});
},
methods: {
loadItems: function(){
var bottomOffset = 2000;
var self = this;
if( $(document).scrollTop() >= ( $(document).height() - $(window).height() - 10 ) && canBeLoaded == true ){
console.log(\'loadItems method fired\');
console.log(page);
canBeLoaded = false;
$.getJSON( \'wp-json/wp/v2/posts?categories=3&page=\'+page+\'&per_page=10&_embed\', function(data){
if( data.length > 0 ){
self.posts = self.posts.concat(data);
page++;
}
}).done(function(){
canBeLoaded = true;
});
}
}, // end loadItems
} // end methods
});
// for debug and prototype only - remove in production
// var root = $(\'html, body\');
// $(\'#scroll-link\').on(\'click\', function(e){
// e.preventDefault();
// root.animate({
// scrollTop: $( $.attr(this, \'href\') ).offset().top
// }, 500);
// return false;
// });
//
}); // doc ready
}(jQuery));
</script>