コンテキスト:私は、ワードプレスapiからのタグ、カテゴリの投稿のリストを持っています。私はVue
でこれらの投稿を表示し、力価、説明、タグ、およびカテゴリ計算されたデータが変更されたとき、Vueは更新されません
問題に基づいて結果をフィルタリングするために、検索ボックスでcomputed
を使用して:ユーザーが使用可能なタグのリストをクリックしたときに私はcomputed
リストを更新しようとしています。
var vm = new Vue({
el: '#blogs',
data: {
search: '',
posts: [],
filterPosts: []
},
beforeMount: function() {
// It should call the data and update
callData();
},
computed: {
filterPosts: {
get: function() {
var self = this;
return self.posts.filter(function(post){
var query = self.search.toLowerCase();
var title = post.title.toLowerCase();
var content = post.content.toLowerCase();
var date = post.date.toLowerCase();
var categories = '';
post.categories.forEach(function(category) {
categories += category.name.toLowerCase();
});
var tags = '';
post.tags.forEach(function(tag){
tags += tag.name.toLowerCase();
});
return title.indexOf(query) !== -1 ||content.indexOf(query) !== -1 || date.indexOf(query) !== -1 || categories.indexOf(query) !== -1 || tags.indexOf(query) !== -1;
});
},
set: function (newValue) {
console.log(newValue);
this.filterPosts = Object.assign({}, newValue);
}
}
},
methods: {
filterByTag: function(tag, event) {
event.preventDefault();
var self = this;
self.filterPosts = self.posts.filter(function(post){
var tags = '';
post.tags.forEach(function(tag){
tags += tag.name.toLowerCase();
});
return tags.indexOf(tag.toLowerCase()) !== -1;
});
}
}
}); // Vue instance
私はmethods
に書きましたが、Vue
が再レンダリングビューではありませんでした機能に基づいてconsole.log
常に出力新しいデータ:私はこのようなcomputed
データのget
とset
を追加します。私は正しい方法をやっていないと思うか、Vue
のように思った。あなたはいくつかの洞察力を与えてくれますか?
編集1 完全コードを追加してください。
私はdata
でfilterPosts
を追加しようとしましたが、私はVueのからこのエラーを受け取っ:
data: {
myValue: 'OK'
},
computed: {
filterPosts: {
get: function() {
return this.myValue + ' is OK'
}
set: function (newValue) {
this.myValue = newValue
}
}
}
より:The computed property "filterPosts" is already defined in data.
クリアしないで、すべての...もっとコード – pirs
を共有し、私は今、いくつかの新しいコード –