深入淺析Vue全局組件與局部組件的區(qū)別
1、組件聲明
<!-- 全局組件模板father模板 --> <template id="father"> <div> <h3>這是{{name}}</h1> <div> <p>這是{{data}}</p> </div> </div> </template> var FATHER = { template: "#father", data: function() { return { name: "一個(gè)全局組件-模板-", data: "數(shù)據(jù):18892087118" } } };
2、組件注冊(cè)
Vue.component('father', FATHER);
3、組件掛載
<h5>全局組件1</h5> <father></father>
4、組件實(shí)例
<!DOCTYPE html> <html> <head> <title>vue2.0 --- 局部組件與全局組件</title> </head> <body> <h3>vue2.0局部組件與全局組件</h3> <div id='app'> <h5>局部組件</h5> <fatherlocal></fatherlocal> <hr> <h5>全局組件1</h5> <father></father> <hr> <h5>全局組件2</h5> <child :fromfather='giveData'></child> </div> <!-- 局部組件模板fatherlocal --> <template id="father-local"> <div> <h3>這是{{name}}</h1> <div> <p>這是{{data}}</p> </div> </div> </template> <!-- 全局組件模板father --> <template id="father"> <div> <h3>這是{{name}}</h1> <div> <p>這是{{data}}</p> </div> </div> </template> <template id="child"> <div> <h3>這是{{name}}</h3> <div> <p>{{cmsgtwo}}</p> <p>{{cmsg}}</p> <p>{{fromfather}}</p> <p>{{fromfather.fmsg}}</p> <p><input type="button" value="按鈕" @click=" "></p> </div> </div> </template> <script src="vue_2.2.2_vue.min.js"></script> <script type="text/javascript"> // 定義組件 var FATHER = { template: "#father", data: function() { return { name: "一個(gè)全局組件-模板-", data: "數(shù)據(jù):18892087118" } } }; var CHILD = { template: "#child", data: function() { return { name: "子組件", cmsg: "子組件里的第一個(gè)數(shù)據(jù)", cmsgtwo: "子組件里的第二個(gè)數(shù)據(jù)" } }, methods: { change: function() { this.fromfather.fmsg = "子組件數(shù)據(jù)被更改了" } }, mounted: function() { this.cmsg = this.fromfather; }, props: ["fromfather"], }; // 注冊(cè)組件 Vue.component('father', FATHER); Vue.component("child", CHILD); var vm = new Vue({ data: { fmsg: "data里的數(shù)據(jù)", giveData: { fmsg: "這是父組件里的數(shù)據(jù)" } }, methods: {}, // 局部組件fatherlocal components: { 'fatherlocal': { template: '#father-local', data: function() { return { name: "局部-父組件", data: "局部-父組件里的數(shù)據(jù)" } } } } }).$mount('#app'); </script> </body> </html>
6、特殊的屬性is
當(dāng)使用 DOM 作為模板時(shí) (例如,將el選項(xiàng)掛載到一個(gè)已存在的元素上),你會(huì)受到 HTML 的一些限制,因?yàn)?Vue 只有在瀏覽器解析和標(biāo)準(zhǔn)化 HTML 后才能獲取模板內(nèi)容。尤其像這些元素<ul>,<ol>,<table>,<select>限制了能被它包裹的元素,而一些像<option>這樣的元素只能出現(xiàn)在某些其它元素內(nèi)部。
自定義組件<my-row>被認(rèn)為是無(wú)效的內(nèi)容,因此在渲染的時(shí)候會(huì)導(dǎo)致錯(cuò)誤。變通的方案是使用特殊的is屬性:
<body> <div id="app1"> <ul> <li is="my-component"></li> </ul> </div> <script> Vue.component("my-component",{ template:"<h1>{{message}}</h1>", data:function(){ return { message:"hello world" } } }); new Vue({ el:"#app1" }) </script> </body>
總結(jié)
以上所述是小編給大家介紹的Vue全局組件與局部組件的區(qū)別,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項(xiàng)目base64加解密使用方式以及解密亂碼
這篇文章主要介紹了vue項(xiàng)目base64加解密使用方式以及解密亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05關(guān)于element-ui中el-form自定義驗(yàn)證(調(diào)用后端接口)
這篇文章主要介紹了關(guān)于element-ui中el-form自定義驗(yàn)證(調(diào)用后端接口),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作
這篇文章主要介紹了ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled
最近在做項(xiàng)目的時(shí)候遇到了些問題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實(shí)現(xiàn)
這篇文章主要介紹了vue 路由視圖 router-view嵌套跳轉(zhuǎn),主要實(shí)現(xiàn)的內(nèi)容有制作一個(gè)登錄頁(yè)面,跳轉(zhuǎn)到首頁(yè),首頁(yè)包含菜單欄、頂部導(dǎo)航欄、主體,標(biāo)準(zhǔn)的后臺(tái)網(wǎng)頁(yè)格式,菜單點(diǎn)擊顯示不同的頁(yè)面,感興趣的小伙伴請(qǐng)參考下面文章內(nèi)容2021-09-09Vue使用Props實(shí)現(xiàn)組件數(shù)據(jù)交互的示例代碼
在Vue中,組件的props屬性用于定義組件可以接收的外部數(shù)據(jù),這些數(shù)據(jù)來(lái)自父組件并傳遞給子組件,本文給大家介紹了Vue使用Props實(shí)現(xiàn)組件數(shù)據(jù)交互,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06vue使用v-for實(shí)現(xiàn)hover點(diǎn)擊效果
hover是css中的選擇器,用于選擇鼠標(biāo)指針浮動(dòng)在上面的元素。這篇文章主要介紹了vue使用v-for實(shí)現(xiàn)hover點(diǎn)擊效果,需要的朋友可以參考下2018-09-09