Kaynağa Gözat

should warn invalid prop keys

Evan You 11 yıl önce
ebeveyn
işleme
5786cb1879

+ 10 - 1
src/directives/prop.js

@@ -1,5 +1,6 @@
 var _ = require('../util')
 var Watcher = require('../watcher')
+var identRE = require('../parsers/path').identRE
 
 module.exports = {
 
@@ -10,6 +11,13 @@ module.exports = {
     var childKey = this.arg
     var parentKey = this.expression
 
+    if (!identRE.test(childKey)) {
+      _.warn(
+        'Invalid prop key: "' + childKey + '". Prop keys ' +
+        'must be valid identifiers.'
+      )
+    }
+
     // simple lock to avoid circular updates.
     // without this it would stabilize too, but this makes
     // sure it doesn't cause other watchers to re-evaluate.
@@ -28,7 +36,8 @@ module.exports = {
       function (val) {
         if (!locked) {
           lock()
-          child.$set(childKey, val)
+          // all props have been initialized already
+          child[childKey] = val
         }
       }
     )

+ 1 - 1
src/parsers/path.js

@@ -1,7 +1,7 @@
 var _ = require('../util')
 var Cache = require('../cache')
 var pathCache = new Cache(1000)
-var identRE = /^[$_a-zA-Z]+[\w$]*$/
+var identRE = exports.identRE = /^[$_a-zA-Z]+[\w$]*$/
 
 /**
  * Path-parsing algorithm scooped from Polymer/observe-js

+ 13 - 0
test/unit/specs/directives/prop_spec.js

@@ -58,6 +58,19 @@ if (_.inBrowser) {
       })
     })
 
+    it('warn invalid keys', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<test a.b.c="{{test}}"></test>',
+        components: {
+          test: {
+            props: ['a.b.c']
+          }
+        }
+      })
+      expect(hasWarned(_, 'Invalid prop key'))
+    })
+
     it('teardown', function (done) {
       var vm = new Vue({
         el: el,