Ver código fonte

Vue.extend: move constructor caching from options to Vue.extend

Evan You 10 anos atrás
pai
commit
c670809521
2 arquivos alterados com 9 adições e 1 exclusões
  1. 8 0
      src/api/global.js
  2. 1 1
      src/util/options.js

+ 8 - 0
src/api/global.js

@@ -44,6 +44,10 @@ var cid = 1
 exports.extend = function (extendOptions) {
   extendOptions = extendOptions || {}
   var Super = this
+  var isFirstExtend = Super.cid === 0
+  if (isFirstExtend && extendOptions._Ctor) {
+    return extendOptions._Ctor
+  }
   var name = extendOptions.name || Super.options.name
   var Sub = createClass(name || 'VueComponent')
   Sub.prototype = Object.create(Super.prototype)
@@ -65,6 +69,10 @@ exports.extend = function (extendOptions) {
   if (name) {
     Sub.options.components[name] = Sub
   }
+  // cache constructor
+  if (isFirstExtend) {
+    extendOptions._Ctor = Sub
+  }
   return Sub
 }
 

+ 1 - 1
src/util/options.js

@@ -232,7 +232,7 @@ function guardComponents (options) {
       def = components[key]
       if (_.isPlainObject(def)) {
         def.name = def.name || key
-        components[key] = def._Ctor || (def._Ctor = _.Vue.extend(def))
+        components[key] = _.Vue.extend(def)
       }
     }
   }