计算过滤不起作用

时间:2017-10-08 18:38:04

标签: vue.js vuejs2

所以,我从2.0开始就给我第一次正确看看Vue.js。而且我很难从filter转到computed

我在这里(使用vue-resource进行API调用):

var moviesURL = 'http://localhost/api/movies';

var app = new Vue({
  el: '#app',

  data: {
    liveFilter: '',
    movies: ''
  },

  methods: {
    getMovies: function() {
      this.$http.get(moviesURL).then(response => {
        this.movies = response.body;
      }, response => {
        console.log(response);
      });
    }
  },

  computed: {
    filteredMovies: function() {
      var self = this
      return this.movies.filter(function(movie) {
        return movie.indexOf(self.liveFilter) !== -1
      });
    }
  },

  created: function() {
    this.getMovies();
  }
});

我不断收到这些错误:

enter image description here

我无法确切地指出我做错了什么......有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您无法过滤字符串。在您的数据中movies应该是一个空数组,而不是空字符串。

另外,请确保response.body也是一个数组。

答案 1 :(得分:1)

您将电影作为空字符串启动,而字符串没有.find()方法。改为使用空数组