В приложении Vuejs 2.6 я использую функцию сравнения для другой сортировки по sort_by
:
export default {
data: function () {
return {
...
sort_by: null,
}
},
computed: {
...
hostelsList: function() {
if ( this.sort_by == null || typeof this.sort_by == "undefined" ) {
this.sort_by= 'price'
}
console.log("this.sort_by::")
console.log( this.sort_by )
function compare(a, b) {
if ( this.sort_by == 'price' ) {
if (a.price < b.price)
return -1;
if (a.price > b.price)
return 1;
} // if ( this.sort_by == 'price' ) {
if ( this.sort_by == 'rating' ) {
if (a.rating < b.rating)
return -1;
if (a.rating > b.rating)
return 1;
} // if ( this.sort_by == 'rating' ) {
if ( this.sort_by == 'name' ) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
} // if ( this.sort_by == 'name' ) {
return 0;
}
return this.$store.getters.hostels.sort(compare);
}, // hostelsList: function() {
Но я получил это ошибка :
app.js?dt=1556086426:98302 [Vue warn]: Error in render: "TypeError: Cannot read property 'sort_by' of undefined"
Модифицированный блок: строки:
console.log("this.sort_by::")
console.log( this.sort_by )
console.log("this::")
console.log( this )
возвращает действительное значение для sort_by
, и я не уверен, что должно быть в этом: ![enter image description here](https://i.stack.imgur.com/NNkya.png)
Полагаю, причина в том, что sort_by
недоступен в функции сравнения, но я не уверен, как передать это значение?
Спасибо!