新聞中心
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Vue中怎么動(dòng)態(tài)創(chuàng)建注冊(cè)component,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
鹿邑ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
常規(guī)組件聲明與注冊(cè)
// 定義一個(gè)名為 button-counter 全局注冊(cè)的組件 Vue.component("button-counter", { template: '', data() { return { count: 0 } } }); new Vue({ template: `` }).$mount("#app");App Component
在上面的代碼中我們聲明了一個(gè)叫做 button-counter 的組件。如果在常規(guī)情況下使用的話,只需要在頁(yè)面上寫(xiě)對(duì)應(yīng)的
全局創(chuàng)建注冊(cè)組件可以實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建,但是我們必須在 template 聲明使用該組件,而且如果把所有組件都全局注冊(cè)這并不是一個(gè)好辦法。
在官方文檔中我們可以看到,我們可以通過(guò) Vue.component('component-name') 的方式來(lái)注冊(cè)一個(gè)組件。
而組件實(shí)例又有 $mount 這樣一個(gè)方法,官方對(duì)于 $mount 的解釋如下:
vm.$mount( [elementOrSelector] )
Arguments:
{Element | string} [elementOrSelector]
{boolean} [hydrating]
Returns: vm - the instance itself
Usage:
If a Vue instance didn't receive the el option at instantiation, it will be in “unmounted” state, without an associated DOM element. vm.$mount() can be used to manually start the mounting of an unmounted Vue instance.
If elementOrSelector argument is not provided, the template will be rendered as an off-document element, and you will have to use native DOM API to insert it into the document yourself.
The method returns the instance itself so you can chain other instance methods after it.
那我們是否可以通過(guò)這種方式來(lái)達(dá)到我們的需求呢?
還不夠!
為什么???
因?yàn)?Vue.component 返回的結(jié)果是一個(gè) function!它返回的并不是 組件實(shí)例,而是一個(gè)構(gòu)造函數(shù)。
那到這里其實(shí)我們就清楚了。 對(duì)于 Vue.component 聲明的組件,我們先通過(guò) Vue.component 獲取它的構(gòu)造函數(shù),再 new 出一個(gè)組件實(shí)例,最后 通過(guò) $mount 掛載到 html 上。
// 定義一個(gè)名為 button-counter 全局注冊(cè)的組件 Vue.component("button-counter", { template: '', data() { return { count: 0 } } }); new Vue({ template: ``, methods: { insert() { const ButtonCounter = Vue.component("button-counter"); // 只能查找到全局注冊(cè)到組件 const instance = new ButtonCounter(); instance.$mount("#insert-container"); } } }).$mount("#app");App Component
上述代碼中,Vue.component 先獲取到組件的構(gòu)造函數(shù),然后構(gòu)造實(shí)例,通過(guò)實(shí)例的一些方法來(lái)處理數(shù)據(jù)和掛載節(jié)點(diǎn)。
但是我們發(fā)現(xiàn) Vue.component 只負(fù)責(zé)全局注冊(cè)或查找。
如果想要針對(duì)局部注冊(cè)的組件使用動(dòng)態(tài)創(chuàng)建并添加我們需要使用 Vue.extend 基礎(chǔ)Vue構(gòu)造器創(chuàng)建"子類"達(dá)到目的。
其實(shí) Vue.component 方法傳入的選項(xiàng)是一個(gè)對(duì)象時(shí),Vue自動(dòng)調(diào)用 Vue.extend。
完整代碼示例:
const ButtonCounterComponent = { template: '', data() { return { count: 0 } } }; new Vue({ template: ``, methods: { insert() { const ButtonCounter = Vue.extend(ButtonCounterComponent); const instance = new ButtonCounter(); instance.$mount("#insert-container"); } } }).$mount("#app");App Component
單文件應(yīng)用
在實(shí)際使用場(chǎng)景里,大部分都是用腳手架構(gòu)建到項(xiàng)目,使用 *.vue 這種單文件方式注冊(cè)組件。
import *.vue 返回的就是模版中 script 中 export 部分。
上述就是小編為大家分享的Vue中怎么動(dòng)態(tài)創(chuàng)建注冊(cè)component了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:Vue中怎么動(dòng)態(tài)創(chuàng)建注冊(cè)component
當(dāng)前URL:http://ef60e0e.cn/article/gscgoc.html