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

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

