Explorar el Código

warn v-for filters that do not return Arrays

Evan You hace 10 años
padre
commit
00568ce251

+ 6 - 0
src/directives/public/for.js

@@ -61,6 +61,12 @@ module.exports = {
   },
 
   update: function (data) {
+    if (process.env.NODE_ENV !== 'production' && !_.isArray(data)) {
+      _.warn(
+        'v-for pre-converts Objects into Arrays, and ' +
+        'v-for filters should always return Arrays.'
+      )
+    }
     this.diff(data)
     this.updateRef()
     this.updateModel()

+ 16 - 0
test/unit/specs/directives/public/for/for_spec.js

@@ -660,6 +660,22 @@ if (_.inBrowser) {
       expect(hasWarned(_, 'It seems you are using two-way binding')).toBe(true)
     })
 
+    it('warn filters that return non-Array values', function () {
+      new Vue({
+        el: el,
+        template: '<div v-for="item in items | test"></div>',
+        data: {
+          items: []
+        },
+        filters: {
+          test: function (val) {
+            return {}
+          }
+        }
+      })
+      expect(hasWarned(_, 'should always return Arrays')).toBe(true)
+    })
+
     it('nested track by', function (done) {
       var vm = new Vue({
         el: el,