Vue.extend( optons );
(1)参数
{ object } options
(2)用法
使用基础Vue构造器创建一个“子类”,其参数是一个包含“组件选项”的对象。data选项是特例,在Vue.extend()中,它必须是函数。
<div id="mount-point"></div>
<!-- 创建构造器 -->
var Profile = Vue.extend({
template:'<p>{
{firstName}} {
{lastName}} aka{
{alias}}</p>',
data:function(){
return{
firstName:'Walter',
lastName:'White',
alias:'Heisenberg'
}
}
})
<!-- 创建Profile实例,并挂载到一个元素上 -->
new Profile().$mount('#mount-point');
(3)全局API和示例方法不同,后者是在Vue的原型上挂载方法,也就是在Vue.prototype上挂载方法,而前者是直接在Vue上挂载方法。
Vue.extend = function(extendOptions){
<!-- 做点什么 -->
}
(4)作用
Vue.extend的作用是创建一个子类,所以可以创建一个子类,然后让它继承Vue身上的一些功能。
二、实现
(1)创建一个子类
Vue.cid = 0
let cid = 1;
Vue.extend = function(extendOptions){
extendOptions = extendOptions || {
};
const Super = this; // 指向 Vue这个构造函数
const SuperId = Super.cid; 指向 Vue这个构造函数的id
const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {
});
if(cachedCtors[SuperId]){
return cachedCtors[SuperId];
}
const name = extendOptions.name || Super.options.name;
if(process.env.NODE_ENV !== 'production'){
if(!/^[a-zA-Z][\w-]*$/.test(<