深入淺析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: "一個全局組件-模板-",
data: "數(shù)據(jù):18892087118"
}
}
};
2、組件注冊
Vue.component('father', FATHER);
3、組件掛載
<h5>全局組件1</h5> <father></father>
4、組件實例
<!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: "一個全局組件-模板-",
data: "數(shù)據(jù):18892087118"
}
}
};
var CHILD = {
template: "#child",
data: function() {
return {
name: "子組件",
cmsg: "子組件里的第一個數(shù)據(jù)",
cmsgtwo: "子組件里的第二個數(shù)據(jù)"
}
},
methods: {
change: function() {
this.fromfather.fmsg = "子組件數(shù)據(jù)被更改了"
}
},
mounted: function() {
this.cmsg = this.fromfather;
},
props: ["fromfather"],
};
// 注冊組件
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
當使用 DOM 作為模板時 (例如,將el選項掛載到一個已存在的元素上),你會受到 HTML 的一些限制,因為 Vue 只有在瀏覽器解析和標準化 HTML 后才能獲取模板內(nèi)容。尤其像這些元素<ul>,<ol>,<table>,<select>限制了能被它包裹的元素,而一些像<option>這樣的元素只能出現(xiàn)在某些其它元素內(nèi)部。
自定義組件<my-row>被認為是無效的內(nèi)容,因此在渲染的時候會導致錯誤。變通的方案是使用特殊的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>
總結
以上所述是小編給大家介紹的Vue全局組件與局部組件的區(qū)別,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
關于element-ui中el-form自定義驗證(調(diào)用后端接口)
這篇文章主要介紹了關于element-ui中el-form自定義驗證(調(diào)用后端接口),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
ant design vue 表格table 默認勾選幾項的操作
這篇文章主要介紹了ant design vue 表格table 默認勾選幾項的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled
最近在做項目的時候遇到了些問題,所以這篇文章主要給大家介紹了關于Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01
vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了vue 路由視圖 router-view嵌套跳轉(zhuǎn),主要實現(xiàn)的內(nèi)容有制作一個登錄頁面,跳轉(zhuǎn)到首頁,首頁包含菜單欄、頂部導航欄、主體,標準的后臺網(wǎng)頁格式,菜單點擊顯示不同的頁面,感興趣的小伙伴請參考下面文章內(nèi)容2021-09-09
Vue使用Props實現(xiàn)組件數(shù)據(jù)交互的示例代碼
在Vue中,組件的props屬性用于定義組件可以接收的外部數(shù)據(jù),這些數(shù)據(jù)來自父組件并傳遞給子組件,本文給大家介紹了Vue使用Props實現(xiàn)組件數(shù)據(jù)交互,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-06-06

