我正在学习Vue,无法理解一件事。我一直收到这个错误:
属性或方法“item”未在实例上定义,但在呈现期间引用。通过初始化属性,确保此属性在数据选项或基于类的组件中是被动的。请参阅:https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。
显然我读过这篇文章并考虑过我的代码,但由于我正在使用Vue的push()方法(应该做的事情),我仍然无法理解为什么它不起作用。有人可以对此有所了解吗?
我在下面分享一些代码:
var maxpages = parseInt(rest_object.maxpages);
Vue.component('news-item', {
template: '#news-template'
,props:['newsdata']
})
var news_manager = new Vue({
el: '#more-news-box'
,data:{
page: 1
,add_more_button: null
,items: []
}
,mounted: function(){
this.add_more_button = $('button.add-more');
}
,methods: {
get_posts: function(){
this.page += 1;
if(this.page > maxpages) return false;
if(this.page == maxpages) this.add_more_button.prop('disabled','disabled').addClass('hidden');
$.ajax({
type: 'GET'
,dataType: 'json'
,url: rest_object.api_url
,data: { per_page: 3 ,page: this.page }
}).done(function(posts_objects) {
_.each(posts_objects, function(post){ this.items.push(post); }.bind(this));
}.bind(this));
}
}
});
HTML:
<div class="more-news-box" id="more-news-box">
<ul><li v-for="(item, index) in items" is="news-item" :key="index" :newsdata="item"></li></ul>
<button class="add-more" type="button" @click="get_posts">add more</button>
</div>
<script type="text/x-template" id="news-template">
<li>
<article class="news-article">
<figure class="news-figure"></figure>
<div class="news-date news-component"></div>
<h3 class="news-title news-component">{{ item.title.rendered }}</h3>
<div class="news-content news-component"></div>
</article>
</li>
</script>
答案 0 :(得分:1)
组件模板引用item
,但item
未在组件中的任何位置声明为属性。
<h3 class="news-title news-component">{{ item.title.rendered }}</h3>
相反,组件声明了newsdata
属性。我希望你想要的是这个:
<h3 class="news-title news-component">{{ newsdata.title.rendered }}</h3>
这里是代码(略有修改,因此API调用有效)。
console.clear()
var maxpages = 10
Vue.component('news-item', {
template: '#news-template'
,props:['newsdata']
})
var news_manager = new Vue({
el: '#more-news-box'
,data:{
page: 1
,add_more_button: null
,items: []
}
,mounted: function(){
this.add_more_button = $('button.add-more');
}
,methods: {
get_posts: function(){
this.page += 1;
if(this.page > maxpages) return false;
if(this.page == maxpages) this.add_more_button.prop('disabled','disabled').addClass('hidden');
$.ajax({
type: 'GET'
,dataType: 'json'
,url: "https://swapi.co/api/people"
,data: { per_page: 3 ,page: this.page }
}).done(function(posts_objects) {
console.log(posts_objects)
_.each(posts_objects.results, function(post){ this.items.push({title:{rendered:post.name}}); }.bind(this));
console.log(this.items)
}.bind(this));
}
}
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/vue@2.4.2"></script>
<div class="more-news-box" id="more-news-box">
<ul><li v-for="(item, index) in items" is="news-item" :key="index" :newsdata="item"></li></ul>
<button class="add-more" type="button" @click="get_posts">add more</button>
</div>
<script type="text/x-template" id="news-template">
<li>
<article class="news-article">
<figure class="news-figure"></figure>
<div class="news-date news-component"></div>
<h3 class="news-title news-component">{{ newsdata.title.rendered }}</h3>
<div class="news-content news-component"></div>
</article>
</li>
</script>
&#13;