Przeglądaj źródła

support mixed prop syntax (close #1798)

Evan You 10 lat temu
rodzic
commit
8ed14c8e4b

+ 8 - 3
src/util/options.js

@@ -247,18 +247,23 @@ function guardComponents (options) {
 
 function guardProps (options) {
   var props = options.props
-  var i
+  var i, val
   if (_.isArray(props)) {
     options.props = {}
     i = props.length
     while (i--) {
-      options.props[props[i]] = null
+      val = props[i]
+      if (typeof val === 'string') {
+        options.props[val] = null
+      } else if (val.name) {
+        options.props[val.name] = val
+      }
     }
   } else if (_.isPlainObject(props)) {
     var keys = Object.keys(props)
     i = keys.length
     while (i--) {
-      var val = props[keys[i]]
+      val = props[keys[i]]
       if (typeof val === 'function') {
         props[keys[i]] = { type: val }
       }

+ 30 - 0
test/unit/specs/directives/internal/prop_spec.js

@@ -438,6 +438,36 @@ if (_.inBrowser) {
       expect(el.textContent).toBe('AAA')
     })
 
+    it('mixed syntax', function () {
+      new Vue({
+        el: el,
+        template: '<test :b="a" :c="d"></test>',
+        data: {
+          a: 'AAA',
+          d: 'DDD'
+        },
+        components: {
+          test: {
+            props: [
+              'b',
+              {
+                name: 'c',
+                type: Number
+              },
+              {
+                name: 'd',
+                required: true
+              }
+            ],
+            template: '<p>{{b}}</p><p>{{c}}</p>'
+          }
+        }
+      })
+      expect(hasWarned(_, 'Missing required prop')).toBe(true)
+      expect(hasWarned(_, 'Expected Number')).toBe(true)
+      expect(el.textContent).toBe('AAA')
+    })
+
     it('should not overwrite default value for an absent Boolean prop', function () {
       var vm = new Vue({
         el: el,