Vue.js 2.0 相同的组件渲染在一个项目上而不是另一个
Vue.js 2.0 same component rendering on one item but not another
我正在构建一个非常类似于 Whosebug 的问答网站,您可以在其中 vote
question
或 answer
。为了简化,我保留了投票代码。
这是 vote
组件用于 question
和 answer
组件。
<template>
<div class="vote">
<h4 @click="voteUp">
<i :class="{'fa fa-chevron-circle-up' : votedUp>-1, 'fa fa-chevron-up': votedUp==-1 }"></i>
</h4>
</div>
</template>
<script>
export default {
props: ['votes','item'],
computed:{
votedUp() {
return _.findIndex(this.votes, {user_id: this.$root.authuser.id});
}
},
methods:{
voteUp() {
axios.post(this.$root.baseUrl+'/vote_item', {item_id:this.item.id,item_model:this.item.model,vote:'up'})
.then(response => {
_.isEmpty(response.data) ? this.votes.splice(this.votedUp,1) : this.votes.push(response.data);
})
}
}
}
</script>
这是使用vote
组件的question
组件:
<template>
<div class="question">
<h1>{{ question.title }}</h1>
<div class="row">
<div class="col-xs-1">
<vote :votes="question.votes" :item="item"></vote>
</div>
<div class="col-xs-11 pb-15">
<p>{{ question.body }}</p>
<comment-list :comments="question.comments" :item="item" ></comment-list>
</div>
</div>
<answer v-for="answer in question.answers" :answer="answer"></answer>
</div>
</template>
<script>
export default {
props: ['question'],
data(){
return {
item:{ id:this.question.id, model:'Question' }
};
}
}
</script>
这是使用vote
组件的answer
组件:
<template>
<div class="answer row">
<div class="col-xs-1 mt-20">
<vote :votes="answer.votes" :item="item"></vote>
</div>
<div class="col-xs-11">
<h3>{{ answer.title }}</h3>
<p>{{ answer.body }}</p>
<comment-list :comments="answer.comments" :item="item" ></comment-list>
</div>
</div>
</template>
<script>
export default {
props: ['answer'],
data(){
return {
item:{ id:this.answer.id, model:'Answer' }
};
}
}
</script>
问题
投票工作正常并改变 question.votes
和 answer.votes
的状态,但它只呈现 answers
的 HTML。我必须刷新才能看到 question
上的赞成票。同样在 Vue 开发人员工具控制台中,answer.votes
会自动刷新,而我需要点击 vue 刷新按钮才能看到 question.votes
考虑了新投票(但仍然没有 HTML 渲染)。
重要提示
如您所见,您还可以在 question
和 answer
上 comment
,这对两者都很好,因为我对 $emit
使用了不同的方法一个事件。也许这是我的答案的解决方案,但我真正想知道的是 为什么 vote
中的功能在 answer
而不是 question
.
谢谢!
您的 vote
组件不应改变 props
。改变本地副本:
export default {
props: ['votes','item'],
data() {
return { votesCopy: [] };
},
mounted() {
this.votesCopy = this.votes.slice();
},
computed:{
votedUp() {
return _.findIndex(this.votesCopy, {user_id: this.$root.authuser.id});
}
},
methods:{
voteUp() {
axios.post(this.$root.baseUrl+'/vote_item', {item_id:this.item.id,item_model:this.item.model,vote:'up'})
.then(response => {
_.isEmpty(response.data)
? this.votesCopy.splice(this.votedUp,1)
: this.votesCopy.push(response.data);
})
}
}
}
我正在构建一个非常类似于 Whosebug 的问答网站,您可以在其中 vote
question
或 answer
。为了简化,我保留了投票代码。
这是 vote
组件用于 question
和 answer
组件。
<template>
<div class="vote">
<h4 @click="voteUp">
<i :class="{'fa fa-chevron-circle-up' : votedUp>-1, 'fa fa-chevron-up': votedUp==-1 }"></i>
</h4>
</div>
</template>
<script>
export default {
props: ['votes','item'],
computed:{
votedUp() {
return _.findIndex(this.votes, {user_id: this.$root.authuser.id});
}
},
methods:{
voteUp() {
axios.post(this.$root.baseUrl+'/vote_item', {item_id:this.item.id,item_model:this.item.model,vote:'up'})
.then(response => {
_.isEmpty(response.data) ? this.votes.splice(this.votedUp,1) : this.votes.push(response.data);
})
}
}
}
</script>
这是使用vote
组件的question
组件:
<template>
<div class="question">
<h1>{{ question.title }}</h1>
<div class="row">
<div class="col-xs-1">
<vote :votes="question.votes" :item="item"></vote>
</div>
<div class="col-xs-11 pb-15">
<p>{{ question.body }}</p>
<comment-list :comments="question.comments" :item="item" ></comment-list>
</div>
</div>
<answer v-for="answer in question.answers" :answer="answer"></answer>
</div>
</template>
<script>
export default {
props: ['question'],
data(){
return {
item:{ id:this.question.id, model:'Question' }
};
}
}
</script>
这是使用vote
组件的answer
组件:
<template>
<div class="answer row">
<div class="col-xs-1 mt-20">
<vote :votes="answer.votes" :item="item"></vote>
</div>
<div class="col-xs-11">
<h3>{{ answer.title }}</h3>
<p>{{ answer.body }}</p>
<comment-list :comments="answer.comments" :item="item" ></comment-list>
</div>
</div>
</template>
<script>
export default {
props: ['answer'],
data(){
return {
item:{ id:this.answer.id, model:'Answer' }
};
}
}
</script>
问题
投票工作正常并改变 question.votes
和 answer.votes
的状态,但它只呈现 answers
的 HTML。我必须刷新才能看到 question
上的赞成票。同样在 Vue 开发人员工具控制台中,answer.votes
会自动刷新,而我需要点击 vue 刷新按钮才能看到 question.votes
考虑了新投票(但仍然没有 HTML 渲染)。
重要提示
如您所见,您还可以在 question
和 answer
上 comment
,这对两者都很好,因为我对 $emit
使用了不同的方法一个事件。也许这是我的答案的解决方案,但我真正想知道的是 为什么 vote
中的功能在 answer
而不是 question
.
谢谢!
您的 vote
组件不应改变 props
。改变本地副本:
export default {
props: ['votes','item'],
data() {
return { votesCopy: [] };
},
mounted() {
this.votesCopy = this.votes.slice();
},
computed:{
votedUp() {
return _.findIndex(this.votesCopy, {user_id: this.$root.authuser.id});
}
},
methods:{
voteUp() {
axios.post(this.$root.baseUrl+'/vote_item', {item_id:this.item.id,item_model:this.item.model,vote:'up'})
.then(response => {
_.isEmpty(response.data)
? this.votesCopy.splice(this.votedUp,1)
: this.votesCopy.push(response.data);
})
}
}
}