vue3 偵聽器 在Vue3中,偵聽器的使用方式與Vue2相同,可以使用watch選項或$watch方法來創建偵聽器。不同之處在於,Vue3中取消了immediate選項,同時提供了新的選項和API。 創建偵聽器 可以使用watch選項或$watch方法來創建偵聽器,語法與Vue2相同。示例如下: ...
在Vue3中,偵聽器的使用方式與Vue2相同,可以使用watch
選項或$watch
方法來創建偵聽器。不同之處在於,Vue3中取消了immediate
選項,同時提供了新的選項和API。
-
創建偵聽器
可以使用watch
選項或$watch
方法來創建偵聽器,語法與Vue2相同。示例如下:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue3!'
}
},
watch: {
message(newValue, oldValue) {
console.log(`New value: ${newValue}, old value: ${oldValue}`)
}
}
}
</script>
上面的代碼中,使用watch
選項來創建偵聽器,當message
的值發生變化時,會觸發偵聽器函數。
-
偵聽多個屬性
在Vue3中,可以使用數組的方式偵聽多個屬性。示例如下:
<template>
<div>{{ fullName }}</div>
</template>
<script>
export default {
data() {
return {
firstName: 'John',
lastName: 'Doe'
}
},
watch: {
['firstName', 'lastName'](newValues, oldValues) {
console.log(`New values: ${newValues}, old values: ${oldValues}`)
}
},
computed: {
fullName() {
return `${this.firstName} ${this.lastName}`
}
}
}
</script>
上面的代碼中,使用數組的方式偵聽firstName
和lastName
兩個屬性,當它們的值發生變化時,會觸發偵聽器函數。
-
深度偵聽
在Vue3中,可以使用deep
選項來實現深度偵聽。示例如下:
<template>
<div>{{ user.name }}</div>
</template>
<script>
export default {
data() {
return {
user: {
name: 'John Doe',
age: 30
}
}
},
watch: {
user: {
handler(newValue, oldValue) {
console.log(`New value: ${JSON.stringify(newValue)}, old value: ${JSON.stringify(oldValue)}`)
},
deep: true
}
}
}
</script>
上面的代碼中,使用deep
選項來實現深度偵聽user
對象的所有屬性,當user
對象的任何屬性發生變化時,都會觸發偵聽器函數。
-
取消偵聽器
在Vue3中,可以使用watch
選項返回的取消函數來取消偵聽器。示例如下:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue3!'
}
},
mounted() {
const unwatch = this.$watch('message', (newValue, oldValue) => {
console.log(`New value: ${newValue}, old value: ${oldValue}`)
})
setTimeout(() => {
unwatch()
}, 5000)
}
}
</script>
上面的代碼中,使用$watch
方法創建偵聽器,並將返回的取消函數存儲在unwatch
變數中,在5秒後調用取消函數,取消偵聽器。
vue3 表單輸入綁定
在Vue3中,表單輸入綁定的方式與Vue2相同,可以使用v-model
指令來實現。不同之處在於,Vue3中取消了.sync
修飾符,同時提供了新的修飾符和API。
-
基本用法
使用v-model
指令可以將表單元素的值與組件的數據進行雙向綁定。示例如下:
<template>
<div>
<input type="text" v-model="message">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: ''
}
}
}
</script>
上面的代碼中,將input
元素的值與message
數據進行雙向綁定,當input
元素的值發生變化時,message
數據也會跟著變化,同時p
元素中展示message
數據的值。
-
修飾符
在Vue3中,提供了新的修飾符來實現更靈活的表單輸入綁定。
-
.lazy
修飾符:在輸入框失去焦點或按下回車鍵後才更新數據。示例如下:
<template>
<div>
<input type="text" v-model.lazy="message">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: ''
}
}
}
</script>
上面的代碼中,使用.lazy
修飾符將輸入框的值在失去焦點或按下回車鍵後才更新message
數據。
-
.trim
修飾符:去除輸入框的首尾空格。示例如下:
<template>
<div>
<input type="text" v-model.trim="message">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: ''
}
}
}
</script>
上面的代碼中,使用.trim
修飾符去除輸入框的首尾空格,並將處理後的值綁定到message
數據上。
-
.number
修飾符:將輸入框的值轉換為數字類型。示例如下:
<template>
<div>
<input type="text" v-model.number="age">
<p>{{ age }}</p>
</div>
</template>
<script>
export default {
data() {
return {
age: 0
}
}
}
</script>
上面的代碼中,使用.number
修飾符將輸入框的值轉換為數字類型,並將轉換後的值綁定到age
數據上。
-
自定義組件
在自定義組件中,可以使用v-model
指令來實現自定義組件的雙向綁定。示例如下:
<template>
<div>
<my-input v-model="message"></my-input>
<p>{{ message }}</p>
</div>
</template>
<script>
import MyInput from './MyInput.vue'
export default {
components: {
MyInput
},
data() {
return {
message: ''
}
}
}
</script>
上面的代碼中,使用v-model
指令將my-input
組件的值與message
數據進行雙向綁定,當my-input
組件的值發生變化時,message
數據也會跟著變化,同時p
元素中展示message
數據的值。需要註意的是,my-input
組件內部需要使用$emit
方法觸發input
事件來實現數據的更新。
vue3 模板引用
在Vue3中,模板引用使用ref
來實現。ref
可以用來獲取組件實例或DOM元素的引用,並將其綁定到組件實例的數據上。
-
組件引用
在Vue3中,使用ref
可以獲取到組件實例的引用。示例如下:
<template>
<div>
<my-component ref="myComponent"></my-component>
</div>
</template>
<script>
import MyComponent from './MyComponent.vue'
export default {
components: {
MyComponent
},
mounted() {
console.log(this.$refs.myComponent) // 輸出組件實例
}
}
</script>
上面的代碼中,使用ref
獲取到my-component
組件的實例,並將其綁定到myComponent
數據上。在mounted
鉤子函數中,可以通過this.$refs.myComponent
獲取到組件實例,併進行操作。
-
DOM元素引用
在Vue3中,使用ref
可以獲取到DOM元素的引用。示例如下:
<template>
<div>
<input type="text" ref="myInput">
</div>
</template>
<script>
export default {
mounted() {
console.log(this.$refs.myInput) // 輸出DOM元素
}
}
</script>
上面的代碼中,使用ref
獲取到input
元素的引用,並將其綁定到myInput
數據上。在mounted
鉤子函數中,可以通過this.$refs.myInput
獲取到DOM元素,併進行操作。
需要註意的是,在Vue3中,ref
只能綁定到組件實例或DOM元素上,不能綁定到普通數據上。
vue3 組件組成
在Vue3中,組件由三部分組成:模板、邏輯和樣式。其中,模板和邏輯與Vue2中的組件相同,而樣式方面,Vue3推薦使用CSS Modules和CSS Variables來實現。
-
模板
組件的模板與Vue2中的模板相同,使用template
標簽來定義。示例如下:
<template>
<div>
<h1>{{ title }}</h1>
<p>{{ content }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello, Vue3!',
content: 'Vue3 is awesome!'
}
}
}
</script>
上面的代碼中,定義了一個簡單的組件模板,包含一個標題和一段文本內容,使用雙花括弧綁定數據。
-
邏輯
組件的邏輯與Vue2中的邏輯相同,使用script
標簽來定義。示例如下:
<template>
<div>
<h1>{{ title }}</h1>
<p>{{ content }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello, Vue3!',
content: 'Vue3 is awesome!'
}
},
methods: {
handleClick() {
console.log('clicked')
}
}
}
</script>
上面的代碼中,定義了一個簡單的組件邏輯,包含一個data
數據對象和一個handleClick
方法。
-
樣式
在Vue3中,推薦使用CSS Modules和CSS Variables來實現組件樣式。CSS Modules可以避免全局樣式的污染,而CSS Variables可以實現更靈活的樣式控制。
使用CSS Modules時,可以在style
標簽中設置module
屬性來啟用CSS Modules。示例如下:
<template>
<div class="wrapper">
<h1 class="title">{{ title }}</h1>
<p class="content">{{ content }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello, Vue3!',
content: 'Vue3 is awesome!'
}
}
}
</script>
<style module>
.wrapper {
padding: 20px;
background-color: #f5f5f5;
}
.title {
font-size: 24px;
color: var(--primary-color);
}
.content {
font-size: 16px;
color: #333;
}
</style>
上面的代碼中,使用CSS Modules設置了.wrapper
、.title
和.content
三個類的樣式,並使用CSS Variables設置了--primary-color
變數的值。
需要註意的是,使用CSS Modules時,類名會被自動轉換為唯一的類名,可以通過$style
來引用。示例如下:
<template>
<div :class="$style.wrapper">
<h1 :class="$style.title">{{ title }}</h1>
<p :class="$style.content">{{ content }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello, Vue3!',
content: 'Vue3 is awesome!'
}
}
}
</script>
<style module>
.wrapper {
padding: 20px;
background-color: #f5f5f5;
}
.title {
font-size: 24px;
color: var(--primary-color);
}
.content {
font-size: 16px;
color: #333;
}
</style>
上面的代碼中,使用$style
引用了.wrapper
、.title
和.content
三個類的樣式。
vue3 組件嵌套關係
在Vue3中,組件嵌套關係與Vue2中的組件嵌套關係相同,通過在模板中嵌套組件來實現。
例如,有兩個組件Parent
和Child
,其中Parent
組件中嵌套了Child
組件。示例如下:
<template>
<div>
<h1>{{ title }}</h1>
<child :content="content"></child>
</div>
</template>
<script>
import Child from './Child.vue'
export default {
components: {
Child
},
data() {
return {
title: 'Parent Component',
content: 'This is the content of Parent Component.'
}
}
}
</script>
上面的代碼中,Parent
組件中通過<child>
標簽嵌套了Child
組件,並將content
數據傳遞給Child
組件。
Child
組件的代碼如下:
<template>
<div>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</template>
<script>
export default {
props: {
content: {
type: String,
default: ''
}
},
data() {
return {
title: 'Child Component'
}
}
}
</script>
上面的代碼中,Child
組件接收了Parent
組件傳遞的content
數據,併在模板中展示出來。
需要註意的是,當組件嵌套層級較深時,可以使用provide
和inject
來實現跨層級傳遞數據,避免層層傳遞數據的麻煩。
vue3 組件註冊方式
在Vue3中,組件註冊方式與Vue2中的組件註冊方式有所不同,Vue3提供了defineComponent
函數來定義組件。具體步驟如下:
-
創建組件
使用defineComponent
函數創建組件,示例如下:
import { defineComponent } from 'vue'
export default defineComponent({
name: 'MyComponent',
props: {
content: {
type: String,
default: ''
}
},
setup(props) {
return {
title: 'My Component',
handleClick() {
console.log('clicked')
}
}
},
template: `
<div>
<h1>{{ title }}</h1>
<p>{{ content }}</p>
<button @click="handleClick">Click Me</button>
</div>
`
})
上面的代碼中,使用defineComponent
函數定義了一個名為MyComponent
的組件,包含props
、setup
和template
三個部分。其中,props
定義了組件的屬性,setup
定義了組件的邏輯,template
定義了組件的模板。
-
註冊組件
使用createApp
函數創建Vue實例,並使用component
方法註冊組件,示例如下:
import { createApp } from 'vue'
import MyComponent from './MyComponent.vue'
const app = createApp()
app.component('my-component', MyComponent)
app.mount('#app')
上面的代碼中,使用component
方法將MyComponent
組件註冊為my-component
組件,並使用mount
方法將Vue實例掛載到DOM節點上。
需要註意的是,使用defineComponent
函數創建的組件可以直接在component
方法中註冊,無需再進行額外的處理。另外,也可以使用defineAsyncComponent
函數定義非同步組件,以優化應用的載入性能。
vue3 組件傳遞數據 props
在Vue3中,組件傳遞數據的方式與Vue2中基本相同,都是通過props
屬性進行傳遞。但是Vue3中對props
進行了一些優化,使得組件傳遞數據更加方便和靈活。
下麵是一個簡單的示例,演示瞭如何在Vue3中使用props
傳遞數據:
// ChildComponent.vue
<template>
<div>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
props: {
title: {
type: String,
required: true
},
content: {
type: String,
default: ''
}
}
})
</script>
// ParentComponent.vue
<template>
<div>
<h1>{{ pageTitle }}</h1>
<child-component :title="childTitle" :content="childContent" />
</div>
</template>
<script>
import { defineComponent } from 'vue'
import ChildComponent from './ChildComponent.vue'
export default defineComponent({
name: 'ParentComponent',
components: {
ChildComponent
},
data() {
return {
pageTitle: 'Parent Component',
childTitle: 'Child Component',
childContent: 'Lorem ipsum dolor sit amet'
}
}
})
</script>
在上面的示例中,ChildComponent
組件定義了兩個props
:title
和content
。title
屬性是必需的,類型為字元串;content
屬性是可選的,類型為字元串,如果沒有傳遞則預設為空字元串。
在ParentComponent
組件中,使用child-component
標簽引入了ChildComponent
組件,並通過:title
和:content
指令將數據傳遞給子組件。在data
中定義了pageTitle
、childTitle
和childContent
三個屬性,分別用於在父組件和子組件中顯示標題和內容。
需要註意的是,在Vue3中,使用props
傳遞數據時,可以通過.sync
修飾符實現雙向綁定,也可以使用v-model
指令簡化雙向綁定的寫法。此外,還可以使用emit
方法向父組件發送事件,實現組件之間的通信。
vue3 組件傳遞多種數據類型
在Vue3中,組件傳遞多種數據類型的方式與Vue2中基本相同,都是通過props
屬性進行傳遞。下麵是一個示例,演示瞭如何在Vue3中使用props
傳遞多種數據類型:
// ChildComponent.vue
<template>
<div>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
<ul>
<li v-for="item in list" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
props: {
title: {
type: String,
required: true
},
content: {
type: String,
default: ''
},
list: {
type: Array,
default: () => []
}
}
})
</script>
在上面的示例中,ChildComponent
組件定義了三個props
:title
、content
和list
。title
屬性是必需的,類型為字元串;content
屬性是可選的,類型為字元串,如果沒有傳遞則預設為空字元串;list
屬性是可選的,類型為數組,如果沒有傳遞則預設為空數組。
在父組件中,可以通過:title
、:content
和:list
指令將數據傳遞給子組件。需要註意的是,如果要傳遞數組類型的數據,可以使用v-bind
指令或簡寫的:
語法,例如:list="[ { id: 1, name: 'item 1' }, { id: 2, name: 'item 2' } ]"
。
需要註意的是,在Vue3中,使用props
傳遞數據時,可以通過.sync
修飾符實現雙向綁定,也可以使用v-model
指令簡化雙向綁定的寫法。此外,還可以使用emit
方法向父組件發送事件,實現組件之間的通信。
vue3 組件傳遞props 校驗
在Vue3中,組件傳遞props
時,可以使用Props
選項進行校驗。Props
選項是一個對象,用於指定組件接受的props
以及其類型、預設值和校驗規則等。
下麵是一個示例,演示瞭如何在Vue3中使用Props
選項進行校驗:
// ChildComponent.vue
<template>
<div>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
<ul>
<li v-for="item in list" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
props: {
// 校驗title屬性,類型為字元串,必須傳遞
title: {
type: String,
required: true
},
// 校驗content屬性,類型為字元串,如果沒有傳遞則預設為空字元串
content: {
type: String,
default: ''
},
// 校驗list屬性,類型為數組,如果沒有傳遞則預設為空數組
list: {
type: Array,
default: () => []
},
// 校驗count屬性,類型為數字,必須大於0
count: {
type: Number,
validator: (value) => value > 0
}
}
})
</script>
在上面的示例中,ChildComponent
組件定義了四個props
:title
、content
、list
和count
。其中,title
和count
屬性是必需的,類型分別為字元串和數字;content
和list
屬性是可選的,類型分別為字元串和數組,如果沒有傳遞則分別預設為空字元串和空數組。此外,count
屬性還定義了一個校驗規則,即必須大於0。
需要註意的是,在Vue3中,如果一個props
屬性沒有指定類型,那麼它可以接受任何類型的數據。如果需要限制props
屬性接受的數據類型,可以使用type
選項指定。如果需要指定多個類型,可以使用數組形式,例如type: [String, Number]
。
此外,如果需要對props
屬性進行更複雜的校驗,可以使用validator
選項。validator
是一個函數,用於校驗props
屬性的值是否符合指定的規則。如果校驗失敗,可以返回false
或拋出異常,Vue會在控制台輸出警告信息。
vue3 組件事件
在Vue3中,組件事件可以使用emits
選項進行定義。emits
選項是一個數組,用於指定組件可以觸發的事件名稱。定義組件事件後,可以使用$emit
方法在組件內部觸發事件,並可以在父組件中使用v-on
指令監聽事件。
下麵是一個示例,演示瞭如何在Vue3中定義組件事件:
// ChildComponent.vue
<template>
<button @click="handleClick">{{ buttonText }}</button>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
emits: ['click'],
props: {
buttonText: {
type: String,
required: true
}
},
methods: {
handleClick() {
this.$emit('click')
}
}
})
</script>
在上面的示例中,ChildComponent
組件定義了一個emits
選項,指定了可以觸發的click
事件。在組件內部,使用$emit
方法觸發click
事件,併在父組件中使用v-on
指令監聽該事件。
下麵是父組件如何監聽ChildComponent
組件觸發的click
事件:
// ParentComponent.vue
<template>
<div>
<ChildComponent :buttonText="buttonText" @click="handleClick" />
</div>
</template>
<script>
import { defineComponent } from 'vue'
import ChildComponent from './ChildComponent.vue'
export default defineComponent({
name: 'ParentComponent',
components: {
ChildComponent
},
data() {
return {
buttonText: 'Click me'
}
},
methods: {
handleClick() {
console.log('ChildComponent clicked')
}
}
})
</script>
在上面的示例中,ParentComponent
組件使用v-on
指令監聽ChildComponent
組件觸發的click
事件,併在handleClick
方法中輸出一條日誌。
需要註意的是,在Vue3中,如果一個組件觸發了未定義的事件,Vue會在控制台輸出警告信息。如果需要禁用這個警告,可以在createApp
方法中傳遞一個config
選項,設置warnHandler
屬性為null
。例如:
import { createApp } from 'vue'
const app = createApp({
// ...
})
app.config.warnHandler = null
app.mount('#app')
vue3 組件事件配合v-model使用
在Vue3中,組件事件可以配合v-model
指令使用,用於實現雙向數據綁定。要實現v-model
指令,需要在組件中定義一個名為modelValue
的prop,併在emits
選項中指定update:modelValue
事件。
以下是一個示例,演示瞭如何在Vue3中使用v-model
指令:
// ChildComponent.vue
<template>
<input :value="modelValue" @input="handleInput" />
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
emits: ['update:modelValue'],
props: {
modelValue: {
type: String,
required: true
}
},
methods: {
handleInput(event) {
this.$emit('update:modelValue', event.target.value)
}
}
})
</script>
在上面的示例中,ChildComponent
組件定義了一個名為modelValue
的prop,併在emits
選項中指定了update:modelValue
事件。在組件內部,使用$emit
方法觸發update:modelValue
事件,並傳遞輸入框的值。
下麵是父組件如何使用v-model
指令綁定ChildComponent
組件的modelValue
:
// ParentComponent.vue
<template>
<div>
<ChildComponent v-model="inputValue" />
</div>
</template>
<script>
import { defineComponent, ref } from 'vue'
import ChildComponent from './ChildComponent.vue'
export default defineComponent({
name: 'ParentComponent',
components: {
ChildComponent
},
setup() {
const inputValue = ref('')
return {
inputValue
}
}
})
</script>
在上面的示例中,ParentComponent
組件使用v-model
指令綁定ChildComponent
組件的modelValue
,並將其賦值給inputValue
變數。此時,ChildComponent
組件的輸入框和inputValue
變數會實現雙向數據綁定。
需要註意的是,v-model
指令實際上是語法糖,相當於同時綁定了一個value
prop和一個update:value
事件。因此,如果需要在組件內部使用v-model
指令,也需要定義一個名為value
的prop,併在emits
選項中指定update:value
事件。
vue3 組件數據傳遞
在 Vue3 中,組件數據傳遞可以通過 props 和 emit 實現。
-
Props
在 Vue3 中,通過 props
定義組件的屬性,可以將數據從父組件傳遞到子組件。父組件中使用子組件時,可以通過 v-bind
或簡寫的 :
來綁定屬性值。
例如,下麵的代碼演示瞭如何使用 props
在父組件中向子組件傳遞數據:
// ChildComponent.vue
<template>
<div>{{ message }}</div>
</template>
<script>
import { defineComponent, PropType } from 'vue'
export default defineComponent({
name: 'ChildComponent',
props: {
message: {
type: String,
required: true
}
}
})
</script>
// ParentComponent.vue
<template>
<div>
<ChildComponent :message="parentMessage" />
</div>
</template>
<script>
import { defineComponent } from 'vue'
import ChildComponent from './ChildComponent.vue'
export default defineComponent({
name: 'ParentComponent',
components: {
ChildComponent
},
data() {
return {
parentMessage: 'Hello from parent component'
}
}
})
</script>
在上面的代碼中,ChildComponent
組件定義了一個名為 message
的 props
,併在模板中使用它來顯示數據。在 ParentComponent
組件中,使用 v-bind
或簡寫的 :
來將父組件的 parentMessage
數據傳遞給子組件的 message
屬性。
-
Emit
在 Vue3 中,通過 emit
發送自定義事件,可以將數據從子組件傳遞到父組件。子組件使用 $emit
方法觸發事件,並傳遞數據。父組件中通過 v-on
或簡寫的 @
來監聽事件,併在事件處理函數中獲取數據。
例如,下麵的代碼演示瞭如何使用 emit
在子組件中向父組件傳遞數據:
// ChildComponent.vue
<template>
<button @click="sendMessage">Send message to parent</button>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',
methods: {
sendMessage() {
this.$emit('message-sent', 'Hello from child component')
}
}
})
</script>
// ParentComponent.vue
<template>
<div>
<ChildComponent @message-sent="handleMessage" />
<div>{{ message }}</div>
</div>
</template>
<script>
import { defineComponent, ref } from 'vue'
import ChildComponent from './ChildComponent.vue'
export default defineComponent({
name: 'ParentComponent',
components: {
ChildComponent
},
setup() {
const message = ref('')
const handleMessage = (data) => {
message.value = data
}
return {
message,
handleMessage
}
}
})
</script>
在上面的代碼中,ChildComponent
組件定義了一個 sendMessage
方法,在方法中使用 $emit
方法觸發 message-sent
事件,並將數據傳遞給父組件。在 ParentComponent
組件中,使用 v-on
或簡寫的 @
來監聽 message-sent
事件,併在事件處理函數中獲取數據。
vue3 透傳Attributes
在 Vue3 中,可以使用 v-bind="$attrs"
透傳父組件的 attributes 到子組件,子組件可以通過 inheritAttrs: false
禁用繼承父組件的 attributes,然後使用 $attrs
獲取透傳的 attributes。
例如,下麵的代碼演示瞭如何使用 $attrs
透傳父組件的 attributes 到子組件:
// ChildComponent.vue
<template>
<div :class="computedClass" v-bind="$attrs">{{ message }}</div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
name: 'ChildComponent',