鍍金池/ 問答/HTML/ Vue.extend與 Vue.util.extend 有啥區(qū)別

Vue.extend與 Vue.util.extend 有啥區(qū)別

Vue.extend與 Vue.util.extend 有啥區(qū)別?
Vue2.0的文檔里面沒有Vue.util相關(guān)的說明啊,但是 weex 的 demo 中有下面的語句
new Vue(Vue.util.extend({ el: '#root', router, store }, App))

回答
編輯回答
伐木累

Vue.extend 和 Vue.util.extend 在 vue.runtime.js 文件中是這么定義的。

  Vue.util = {
    warn: warn,
    extend: extend,
    mergeOptions: mergeOptions,
    defineReactive: defineReactive$$1
  };

 /**
  * Mix properties into target object.
  */
  function extend (to, _from) {
    for (var key in _from) {
      to[key] = _from[key];
    }
    return to
  }

  /**
   * Class inheritance
   */
  Vue.extend = function (extendOptions) {
    extendOptions = extendOptions || {};
    var Super = this;
    
    /* ... */
    
    var Sub = function VueComponent (options) {
      this._init(options);
    };
    
    /* ... */
    
    return Sub
  };
    

Vue.util.extend 的主要作用就是合并 { el: '#root', router, store } App就這么簡單。

2017年6月28日 15:52