也就是說,假如你的應(yīng)用里面就只有這一個組件實例的話,那么你完全可以不用擔(dān)心所有組件實例共享數(shù)據(jù)的問題。即
var childData={ option1:'', option2:'', date:''
} var child=Vue.extend({ tempalte:'#demoTemplate', data:childData,
}) var father=new Vue({ el:'#fatherId', data:{ childData:childData,
...
}, components:{//局部注冊
ch:child
}
})
這樣父子共同使用childData的數(shù)據(jù)
/*子組件定義*/
var childObj=Vue.extend({ template:'#demoTp', props:['father']
});
<!--子組件在他爹里面的樣子-->
<div id="father">
<!--:是v-bind,必須要要加上去-->
<component :is="child" :father="msg"></component>
</div>
/*定義他爹*/
var father = new Vue({ el:'#father', data:{ child:'ch'//子組件的名字
msg:''//給子組件的數(shù)據(jù)放這里
childData:{ option1:'', option2:'',
option3:'', date:'',
}
}, components:{ ch:childObj//注冊 ch,子組件的名字
}, ready:function(){ this.msg=this.childData//你可以這樣給子組件傳值
}
})
<!--子組件內(nèi)容-->
<template id="demoTp">
<input type="text" v-model="father.option1" />
<input type="text" v-model="father.option2" />
<input type="text" v-model="father.option3" />
<p>{{father.option3}}</p>
<p>這里的father就是組件掛載點上那個father屬性</p>
</tempalte>