vue3組件化開發(fā)常用API知識(shí)點(diǎn)總結(jié)
組件化思想
為什么使用組件化開發(fā)?
當(dāng)前前端比較流行的 Vue React 等框架,都會(huì)通過編寫組件來完成業(yè)務(wù)需求,也就是組件化開發(fā)。包括小程序開發(fā)也會(huì)用到組件化開發(fā)的思想。
分析組件化思想開發(fā)應(yīng)用程序:
- 將一個(gè)完整頁面拆分成很多個(gè)小組件
- 每個(gè)組件用于完成頁面的一個(gè)功能模塊
- 每一個(gè)組件還可以細(xì)分 (父子組件)
- 通用的組件可以復(fù)用到不同的頁面
一個(gè) Vue 的頁面,應(yīng)該像是棵嵌套的組件樹的形式來組織:

vue3 入口文件:
import { createApp } from 'vue';
import App from './App.vue';
createApp(App).mount('#app');
createApp()函數(shù)傳入了一個(gè)App,App 就是一個(gè)組件,是項(xiàng)目的根組件。
下面將分析 Vue3 中組件化開發(fā)的常用方法。
組件通訊
$props
$props用于向子組件傳遞數(shù)據(jù)
<p> $props: {{$props}} </p>
<script setup>語法糖中需要使用definePropsApi獲取props
const props = defineProps({
num: Number,
})
$emits
$emit用于調(diào)用父級組件的方法
<button @click="$emit('add')">
add
</button>
<script setup>語法糖中需要使用defineEmitsApi聲明emits
<button @click="add">{{ num }}</button>
const emits = defineEmits(['add'])
function add() {
emits('add')
}
$parent
$parent用于獲取父組件實(shí)例對象。
<script setup> 中組件實(shí)例不會(huì)暴露出來,直接在模板中使用$parent會(huì)返回一個(gè)空對象。
為了在 <script setup> 組件中明確要暴露出去的屬性,使用 defineExpose 編譯器宏:
const parData = ref("父組件數(shù)據(jù)");
defineExpose({
parData,
})
子組件:
<p>父組件 parData: {{$parent.parData}}</p>
$attrs
- 包含了父作用域中不作為組件
props或自定義事件的 attribute 綁定和事件。
子組件:
<Foo a="a" b="b" :num="num" @add="add" />
在父組件中,$attrs 的值就是 { "a": "a", "b": "b" }。
- 當(dāng)一個(gè)組件沒有聲明任何 prop 時(shí),這里會(huì)包含所有父作用域的綁定,并且可以通過
v-bind="$attrs"傳入內(nèi)部組件——這在創(chuàng)建高階的組件時(shí)會(huì)非常有用,舉個(gè)例子:
父組件:
<Bar :age=18 sex="boy" />
子組件 Bar.vue
<p v-bind="$attrs">將$attrs對象綁定到當(dāng)前標(biāo)簽</p>
在瀏覽器查看DOM,age 和 sex作為屬性被綁定到了這個(gè)p標(biāo)簽上面。
<script setup>中,需要使用useAttrs
import { useAttrs } from 'vue';
const attrs = useAttrs();
console.log(attrs.sex); // boy
proviede & inject
- 用于跨層級/多層級的組件通信
- 父組件有一個(gè)
provide選項(xiàng)來提供數(shù)據(jù),子組件有一個(gè)inject選項(xiàng)來開始使用這些數(shù)據(jù)。
父級組件:
provide("user", "kong");
provide("pass", 123456);
子孫級組件:
const user = inject("user");
const pass = inject("pass");
插槽 slot
用于內(nèi)容分發(fā),將 <slot> 元素作為承載分發(fā)內(nèi)容的出口。
SlotComp 組件
<template>
<div :style="s1">
<slot name="head"></slot>
</div>
<div :style="s2">
<slot name="foot"></slot>
</div>
</template>
使用上面的組件
<SlotComp>
<template v-slot:head>
<p>head插槽</p>
</template>
<template v-slot:foot>
<p>foot插槽</p>
</template>
</SlotComp>
SlotComp 組件中帶 name屬性的 slot插槽內(nèi)容,會(huì)被替換。被替換的內(nèi)容 需要在父組件中使用 v-slot指令為插槽提供想要顯示的內(nèi)容。
渲染作用域
<template v-slot:foot>
<p>foot插槽</p>
{{msg}}
{{items}}
</template>
上面的例子,{{items}} 不會(huì)顯示數(shù)據(jù)。
父級模板里的所有內(nèi)容都是在父級作用域中編譯的;子模板里的所有內(nèi)容都是在子作用域中編譯的。
作用域插槽
讓插槽的內(nèi)容訪問子組件才有的數(shù)據(jù):
<slot name="head" :item="items"></slot>
插槽內(nèi)容:
<template v-slot:head = "slotProps">
<p v-for="i in slotProps.item">{{i}}</p>
</template>
綁定在 <slot> 元素上的 attribute 被稱為插槽 prop?,F(xiàn)在,在父級作用域中,我們可以使用帶值的 v-slot 來定義我們提供的插槽 prop 的名字,本例中就是slotProps。
v-model
表單組件
- 將
v-model應(yīng)用在表單上面,實(shí)現(xiàn)雙向綁定:
<input v-model="text" />
自定義組件
- 將
v-model應(yīng)用在自定義組件上面:
父組件中使用自定義組件:
const msg = ref('hello world!');
<ModelComp v-model="msg"></ModelComp>
相當(dāng)于:
<ModelComp
:modelValue="msg"
@update:modelValue="msg = $event"
>
</ModelComp>
自定義組件ModelComp.vue中:
const props = defineProps({
modelValue: String
})
const emits = defineEmits([
"update:modelValue"
])
const text = ref("請輸入..");
// text改變時(shí)執(zhí)行
watch(text,()=>{
emits("update:modelValue",text.value);
})
改變默認(rèn)參數(shù)
- 未設(shè)置參數(shù)時(shí)如上,默認(rèn)綁定的參數(shù)是
modelValueupdate:modelValue
設(shè)置v-model參數(shù):
<ModelComp v-model:show="show"></ModelComp>
相當(dāng)于:
<ModelComp
:show="showFlag"
@update:show="showFlag = $event"
>
</ModelComp>
自定義組件ModelComp.vue中:
const props = defineProps({
show: Boolean
})
const emits = defineEmits([
"update:show",
])
樣式綁定相關(guān)
class
class綁定:根據(jù)需求動(dòng)態(tài)綁定class樣式時(shí)可以使用一下幾種方法
寫法1:對象語法
<button @click="changeColor" :class="{ active: isActive }">
點(diǎn)擊切換我的文體顏色樣式
</button>
const isActive = ref(false);
const changeColor = () => {
isActive.value = !isActive.value;
}
寫法2:對象語法
<button @click="changeColor2" :class="classObj">
點(diǎn)擊切換顏色,根據(jù)計(jì)算屬性
</button>
const isActive2 = reactive({
active: false,
})
const classObj = computed(() => {
return {
active: isActive2.active,
'font-30': true,
}
})
const changeColor2 = () => {
isActive2.active = !isActive2.active
}
寫法3:數(shù)組語法
<div :class="[activeClass, errorClass]"></div>
三目運(yùn)算符寫法
<div :class="[isActive ? activeClass : '', errorClass]"></div>
數(shù)組語法中結(jié)合對象語法使用
<div :class="[{ active: isActive }, errorClass]"></div>
style
動(dòng)態(tài)綁定行內(nèi)style樣式
三種方式:html代碼
<p :style="{ color: colorRed }">style綁定</p>
<p :style="styleObj">style對象綁定(直接綁定到一個(gè)對象,代碼更清新)</p>
<p :style="[styleObj, styleObjRed]">style數(shù)組綁定</p>
js 代碼
const colorRed = ref('#f00')
const styleObj = reactive({
fontSize: '30px',
})
const styleObjRed = reactive({
color: '#f00',
})
代碼demo地址 github.com/kongcodes/v…
總結(jié)
到此這篇關(guān)于vue3組件化開發(fā)常用API知識(shí)點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)vue3組件化開發(fā)API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DeepSeek?助力?Vue?開發(fā)絲滑的表單驗(yàn)證Form?Validation功能
文章介紹了如何使用Vue3的組合式API創(chuàng)建一個(gè)表單驗(yàn)證組件,并提供了詳細(xì)的代碼示例,組件支持雙向綁定、自定義驗(yàn)證規(guī)則、樣式和布局等功能,還涵蓋了組件的調(diào)用示例、路由配置和頁面展示入口2025-02-02
vue實(shí)現(xiàn)調(diào)取手機(jī)攝像頭和相冊功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)調(diào)取手機(jī)攝像頭和相冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue實(shí)現(xiàn)樹形結(jié)構(gòu)增刪改查的示例代碼
其實(shí)很多公司都會(huì)有類似于用戶權(quán)限樹的增刪改查功能,本文主要介紹了vue實(shí)現(xiàn)樹形結(jié)構(gòu)增刪改查,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Vue使用localStorage存儲(chǔ)數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue使用localStorage存儲(chǔ)數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

