VUE3學(xué)習(xí)教程之全局組件和局部組件
1. 概述
老話說的好:忍耐是一種策略,同時(shí)也是一種性格磨煉。
言歸正傳,今天我們來聊聊 VUE 的全局組件與局部組件。
2. 全局組件
2.1 不使用組件的寫法
<body>
<div id="myDiv"></div>
</body>
<script>
const app = Vue.createApp({
template:`
<div>
<div>hello</div>
<div>zhuifengren</div>
</div>
`
});
const vm = app.mount("#myDiv");這是我們之前不使用組件的寫法,接下來我們用組件去實(shí)現(xiàn)相同的效果。

2.2 使用組件
const app = Vue.createApp({
template:`
<div>
<hello-com />
<zhuifengren-com />
</div>
`
});
app.component('hello-com', {
template: `
<div>hello</div>
`
});
app.component('zhuifengren-com', {
template: `
<div>zhuifengren</div>
`
});我們把之前的<div>hello</div> 和<div>zhuifengren</div> 分別封裝在了組件中,然后直接將組件名作為標(biāo)簽即可。
組件名稱的命名規(guī)范:建議使用全小寫字母,單詞之間使用 “-” 連接。
2.3 組件中包含變量
const app = Vue.createApp({
template:`
<div>
<count-com />
</div>
`
});
app.component('count-com', {
data() {
return {
count : 1
}
},
template: `
<div>{{count}}</div>
<button @click="count += 1">加1</button>
`
});
2.4 組件的復(fù)用
const app = Vue.createApp({
template:`
<div>
<count-com />
<count-com />
<count-com />
</div>
`
});從這個(gè)例子能看出來,組件的好處就是可以復(fù)用,且組件中的變量是獨(dú)享的。

2.5 組件中使用組件
const app = Vue.createApp({
template:`
<div>
<count-com />
<count-com />
<count-com />
<count-com-2 />
</div>
`
});
app.component('count-com-2', {
template: `
<count-com />
`
});我們在另一個(gè)組件 count-com-2 中使用 count-com 組件,也是可以的。

2.6 總結(jié)
全局組件,使用起來很簡單,只需要使用 app.component 函數(shù)聲明一下即可。
一個(gè)全局組件可以被另一個(gè)全局組件使用。
但全局組件,只要聲明,即使不使用也會被初始化,影響性能。
3. 局部組件
3.1 局部組件的使用
<body>
<div id="myDiv"></div>
</body>
<script>
const CountCom = {
data() {
return {
count : 1
}
},
template: `
<div>{{count}}</div>
<button @click="count += 1">自增</button>
`
}
const app = Vue.createApp({
// 組件映射
components : {
'count-com': CountCom
},
template:`
<div>
<count-com/>
</div>
`
});
const vm = app.mount("#myDiv");局部組件的寫法是,首先聲明一個(gè)對象,內(nèi)容和全局組件類似,然后將組件與對象做一個(gè)映射。

3.2 總結(jié)
局部組件聲明的對象建議首字母大寫,單詞間使用駝峰命名。
映射時(shí),組件的名稱還保持全小寫字母,單詞之間使用 “-” 連接。
局部組件,如果不使用,就不會初始化,因此對性能有好處。
附:vue 3 組件的分類 全局組件與局部組件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>組件的分類</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="itany">
<my-hello></my-hello>
<my-world></my-world>
</div>
<script>
/**
* 全局組件,可以在所有vue實(shí)例中使用
*/
Vue.component('my-hello',{
template:'<h3>{{name}}</h3>',
data:function(){ //在組件中存儲數(shù)據(jù)時(shí),必須以函數(shù)形式,函數(shù)返回一個(gè)對象
return {
name:'alice'
}
}
});
/**
* 局部組件,只能在當(dāng)前vue實(shí)例中使用
*/
var vm=new Vue({
el:'#itany',
data:{
name:'tom'
},
components:{ //局部組件
'my-world':{
template:'<h3>{{age}}</h3>',
data(){
return {
age:25
}
}
}
}
});
</script>
</body>
</html>總結(jié)
今天聊了一下 VUE3 的 全局組件與局部組件,希望可以對大家的工作有所幫助
到此這篇關(guān)于VUE3學(xué)習(xí)教程之全局組件和局部組件的文章就介紹到這了,更多相關(guān)VUE3全局組件和局部組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決打包后出現(xiàn)錯(cuò)誤y.a.addRoute is not a function的
這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
axios全局注冊,設(shè)置token,以及全局設(shè)置url請求網(wǎng)段的方法
今天小編就為大家分享一篇axios全局注冊,設(shè)置token,以及全局設(shè)置url請求網(wǎng)段的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue解析Json數(shù)據(jù)獲取Json里面的多個(gè)id問題
這篇文章主要介紹了vue解析Json數(shù)據(jù)獲取Json里面的多個(gè)id問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
詳解使用mpvue開發(fā)github小程序總結(jié)
這篇文章主要介紹了詳解使用mpvue開發(fā)github小程序總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
vue里input根據(jù)value改變背景色的實(shí)例
今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue-cli的webpack模板項(xiàng)目配置文件分析
本篇文章主要對vue-cli的webpack模板項(xiàng)目配置文件進(jìn)行分析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04

