0
子コンポーネントを使用しているときに問題が発生しています。Vue.js:子コンポーネントを使用しているときにリストが更新されない
comments
の配列データが変更された場合、子コンポーネント<comment></comment>
を使用するとリストは更新されません。
TopHeaderテンプレート:
<template>
<ul v-for="comment in comments">
// If don't use a child component, it updates whenever `comments` array changes:
<li>
<div>
/r/{{comment.data.subreddit}} ·
{{comment.data.score}}
</div>
<div class="comment" v-html="comment.data.body"></div>
<hr>
</li>
</ul>
</template>
TopHeaderコンポーネント:
import Comment from 'components/Comment'
export default {
name: 'top-header',
components: {
Comment
},
data() {
return {
username: '',
comments: []
}
},
methods: {
fetchData: function(username){
var vm = this;
this.$http.get(`https://www.reddit.com/user/${username}/comments.json?jsonp=`)
.then(function(response){
vm.$set(vm, 'comments', response.body.data.children);
});
}
}
}
私は子コンポーネントを使用している場合しかし、それは更新されません。
修正TopHeaderテンプレート:
<template>
<ul v-for="comment in comments">
// If I instead use a component with prop data, it does not update
<comment :data="comment.data"></comment>
</ul>
</template>
コメント子テンプレート:
<template>
<li>
<div>
/r/{{subreddit}} ·
{{score}}
</div>
<div class="comment" v-html="body"></div>
<hr>
</li>
</template>
コメント子コンポーネントは:
export default {
name: 'comment',
props: ['data'],
data() {
return {
body: this.data.body,
subreddit: this.data.subreddit,
score: this.data.score,
}
}
}
おそらく 'ul'タグ内に無効なタグがあるためです。 [DOM Template Parsing Cavesats](https://vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats)を参照してください。 –
@MathewJibinは、ulとliの無効なタグについては正しいですが、実際の問題はここにはないと思います。データモデル内に動的データを割り当てることに関連している可能性があります。あなたの場合。 コンソールにエラーがありますか? –
@MathewJibin -
はまだ同じ問題があります – frosty