Vue動態(tài)創(chuàng)建注冊component的實例代碼
前言
在深入了解Vue動態(tài)創(chuàng)建Component前先了解一下常規(guī)組件聲明形式。
Vue 的組件通??梢酝ㄟ^兩種方式來聲明,一種是通過 Vue.component,另外一種則是 Single File Components(SFC) 單文件組件 。
常規(guī)組件聲明與注冊
// 定義一個名為 button-counter 全局注冊的組件
Vue.component("button-counter", {
template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
data() {
return {
count: 0
}
}
});
new Vue({
template: `
<div id="app">
<h1>App Component</h1>
<button-counter></button-counter>
</div>
`
}).$mount("#app");
在上面的代碼中我們聲明了一個叫做 button-counter 的組件。如果在常規(guī)情況下使用的話,只需要在頁面上寫對應(yīng)的 <button-counter></button-counter> 標(biāo)簽就夠了。
全局創(chuàng)建注冊組件可以實現(xiàn)動態(tài)創(chuàng)建,但是我們必須在 template 聲明使用該組件,而且如果把所有組件都全局注冊這并不是一個好辦法。
在官方文檔中我們可以看到,我們可以通過 Vue.component('component-name') 的方式來注冊一個組件。
而組件實例又有 $mount 這樣一個方法,官方對于 $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.
那我們是否可以通過這種方式來達(dá)到我們的需求呢?
還不夠!
為什么???
因為 Vue.component 返回的結(jié)果是一個 function!它返回的并不是 組件實例,而是一個構(gòu)造函數(shù)。
那到這里其實我們就清楚了。 對于 Vue.component 聲明的組件,我們先通過 Vue.component 獲取它的構(gòu)造函數(shù),再 new 出一個組件實例,最后 通過 $mount 掛載到 html 上。
// 定義一個名為 button-counter 全局注冊的組件
Vue.component("button-counter", {
template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
data() {
return {
count: 0
}
}
});
new Vue({
template: `
<div id="app">
<h1>App Component</h1>
<button @click="insert">click to insert button-counter comonent</button>
<div id="insert-container"></div>
</div>
`,
methods: {
insert() {
const ButtonCounter = Vue.component("button-counter"); // 只能查找到全局注冊到組件
const instance = new ButtonCounter();
instance.$mount("#insert-container");
}
}
}).$mount("#app");
上述代碼中,Vue.component 先獲取到組件的構(gòu)造函數(shù),然后構(gòu)造實例,通過實例的一些方法來處理數(shù)據(jù)和掛載節(jié)點。
但是我們發(fā)現(xiàn) Vue.component 只負(fù)責(zé)全局注冊或查找。
如果想要針對局部注冊的組件使用動態(tài)創(chuàng)建并添加我們需要使用 Vue.extend 基礎(chǔ)Vue構(gòu)造器創(chuàng)建"子類"達(dá)到目的。
其實 Vue.component 方法傳入的選項是一個對象時,Vue自動調(diào)用 Vue.extend。
完整代碼示例:
const ButtonCounterComponent = {
template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>',
data() {
return {
count: 0
}
}
};
new Vue({
template: `
<div id="app">
<h1>App Component</h1>
<button @click="insert">click to insert button-counter comonent</button>
<div id="insert-container"></div>
</div>
`,
methods: {
insert() {
const ButtonCounter = Vue.extend(ButtonCounterComponent);
const instance = new ButtonCounter();
instance.$mount("#insert-container");
}
}
}).$mount("#app");
單文件應(yīng)用
在實際使用場景里,大部分都是用腳手架構(gòu)建到項目,使用 *.vue 這種單文件方式注冊組件。
<template></template>
<script>
export default {
data() {
return {
msg: "hello"
}
},
created() {},
mounted() {},
destroy() {}
};
</script>
<style scoped></style>
import *.vue 返回的就是模版中 script 中 export 部分。
總結(jié)
至此,我們知道了,全局組件動態(tài)注冊 和 局部組件動態(tài)注冊 的使用方法和注意事項,我們可以結(jié)合實際情況去選擇不同方案進(jìn)行搭配即可。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Vue表格首列相同數(shù)據(jù)合并實現(xiàn)方法
這篇文章主要介紹了Vue實現(xiàn)表格首列相同數(shù)據(jù)合并的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Vue3使用ref和reactive管理狀態(tài)的代碼示例
ref 和 reactive 是 Composition API 中用來聲明響應(yīng)式數(shù)據(jù)的兩個核心函數(shù),在 Vue 3 中,使用 setup 語法糖可以更簡潔地使用這些功能,本文將探討如何使用 ref 和 reactive 來管理狀態(tài),并解釋它們之間的區(qū)別,需要的朋友可以參考下2024-09-09
vue router使用query和params傳參的使用和區(qū)別
本篇文章主要介紹了vue router使用query和params傳參的使用和區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
關(guān)于Element?table組件滾動條不顯示的踩坑記錄
這篇文章主要介紹了關(guān)于Element?table組件滾動條不顯示的踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue.js如何利用v-for循環(huán)生成動態(tài)標(biāo)簽
這篇文章主要給大家介紹了關(guān)于Vue.js如何利用v-for循環(huán)生成動態(tài)標(biāo)簽的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-01-01
Vue使用pinia管理數(shù)據(jù)pinia持久化存儲問題
這篇文章主要介紹了Vue使用pinia管理數(shù)據(jù)pinia持久化存儲問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

