vue3 setup中defineEmits與defineProps的使用案例詳解
一、defineEmits的使用
使用說明
1、在子組件中調(diào)用defineEmits并定義要發(fā)射給父組件的方法
const emits = defineEmits(['foldChange'])
2、使用defineEmits會(huì)返回一個(gè)方法,使用一個(gè)變量emits(變量名隨意)去接收
3、在子組件要觸發(fā)的方法中,調(diào)用emits并傳入發(fā)射給父組件的方法以及參數(shù)
emits('foldChange', isFold.value)1.子組件定義:
<template>
<div class="nav-header">
<el-icon size="25" class="fold-menu" @click="handleFoldClick">
<component :is="`${isFold ? 'Fold' : 'Expand'}`"></component>
</el-icon>
<!-- <el-icon><Fold /></el-icon> -->
<!-- <Expand -->
</div>
</template>
<script setup lang="ts">
import { ref, defineEmits } from 'vue'
// 定義發(fā)射給父組件的方法
const emits = defineEmits(['foldChange'])
const isFold = ref(false)
const handleFoldClick = () => {
isFold.value = !isFold.value
emits('foldChange', isFold.value)
}
</script>2.父組件接收使用:
<template>
<div class="main">
<el-container class="main-content">
<el-aside :width="isCollapse ? '60px' : '210px'">
<nav-menu :collapse="isCollapse"></nav-menu>
</el-aside>
<el-container class="page">
<el-header class="page-header">
<nav-header @foldChange="handleFoldChange"></nav-header>
</el-header>
<el-main class="page-content">Main</el-main>
</el-container>
</el-container>
</div>
</template>
<script lang="ts" setup>
import NavMenu from '@/components/nav-menu'
import NavHeader from '@/components/nav-header'
import { ref } from 'vue'
const isCollapse = ref(false)
const handleFoldChange = (isFold: boolean) => {
isCollapse.value = isFold
}
</script>二、 defineProps的使用
使用說明
1、在父組件中定義String、Number、Boolean、Array、Object、Date、Function、Symbol這些類型的數(shù)據(jù)
2、在子組件中通過defineProps API來進(jìn)行接受
3、通過子組件事件修改變量值,同時(shí)將值傳遞給父組件,對(duì)父組件的變量進(jìn)行賦值
4、向子組件傳遞非props的屬性,用法及效果如下
1.1 子組件定義 方式一
<template>
<h3 v-bind="$attrs">字符串: {{props.str}}</h3>
<h3>數(shù)字: {{props.num}}</h3>
<h3>布爾: {{props.bool}}</h3>
<h3>數(shù)組: {{props.arr}}</h3>
<h3>對(duì)象: {{props.obj}}</h3>
<h3>日期: {{props.date}}</h3>
<h3>Symbol: {{props.a}} - {{props.b}}</h3>
</template>
<script setup>
import { defineProps } from 'vue'
const props = defineProps({
str: String,
num: Number,
bool: Boolean,
arr: Array,
obj: Object,
date: Date,
getConsole: Function,
message: Object,
a: Symbol,
b: Symbol
})
props.getConsole()
</script>1.2 子組件定義 方式二
<template>
<div class="shopList">
<div class="shopContent"
:class="{tabActive: currentIndex === index }"
v-for="(tab, index) in tabBars" :key="index"
@click="itemClick(index)">
{{tab.name}}
</div>
</div>
</template>
<script setup>
import { defineProps,ref,defineEmits } from 'vue'
// 接受父組件傳遞的數(shù)據(jù)
const props = defineProps({
tabBar: {
type: Array,
default: () => []
}
})
// 定義屬性
const currentIndex = ref(0)
const tabBars = JSON.parse(JSON.stringify(props.tabBar))
// 定義發(fā)射給父組件的方法
const emits = defineEmits(['tabClick'])
// tab點(diǎn)擊的方法
const itemClick = (e) => {
currentIndex.value = e
emits('tabClick', currentIndex.value)
}
</script>
<style lang="scss" scoped>
.shopList {
display: flex;
justify-content: center;
align-items: center;
.shopContent {
flex: 1;
text-align: center;
padding: 20px;
cursor: pointer;
}
.tabActive {
border-bottom: 3px solid #bf0706;
color: #bf0706;
}
}
</style>2、父組件使用
<template>
<showMessage
:str="str"
:num="num"
:bool="bool"
:arr="arr"
:obj="obj"
:date="date"
:a = "a"
:b="b"
:getConsole="getConsole"
id="abc"
class="bcd"
></showMessage>
</template>
<script setup>
import showMessage from './ShowMessage.vue'
// 定義屬性
const str = '吃飯、睡覺、敲代碼'
const num = 100
const bool = true
const arr = ['apple', 'lemon', 'orange']
const obj = {
name: 'coderXiao',
age: 18
}
const date = new Date()
const a = Symbol('好好學(xué)習(xí)')
const b = Symbol('天天向上')
// 定義方法
const getConsole = () => {
console.log('傳遞給子組件的方法');
}
</script>
<style lang="scss" scoped>
</style>總結(jié)
到此這篇關(guān)于vue3 setup中defineEmits與defineProps的使用案例的文章就介紹到這了,更多相關(guān)vue3 setup defineEmits與defineProps內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)的相關(guān)資料,表單驗(yàn)證能通過設(shè)置驗(yàn)證規(guī)則驗(yàn)證用戶的輸入,并對(duì)不規(guī)范的輸入做出對(duì)應(yīng)提示,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Vue項(xiàng)目報(bào)錯(cuò):Uncaught SyntaxError: Unexpected token <
這篇文章主要介紹了Vue項(xiàng)目報(bào)錯(cuò):Uncaught SyntaxError: Unexpected token <,在引入第三方依賴的 JS 文件時(shí),遇到的一個(gè)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Vue項(xiàng)目新一代狀態(tài)管理工具Pinia的使用教程
pinia是一個(gè)輕量級(jí)的狀態(tài)管理庫,屬于 vue3 生態(tài)圈新的成員之一,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目新一代狀態(tài)管理工具Pinia的使用教程,需要的朋友可以參考下2022-11-11
ElementUI 詳細(xì)分析DatePicker 日期選擇器實(shí)戰(zhàn)
這篇文章主要介紹了ElementUI詳細(xì)分析DatePicker 日期選擇器實(shí)戰(zhàn)教程,本文通過實(shí)例代碼圖文介紹給大家講解的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼(行列互換)
這篇文章主要介紹了el-table實(shí)現(xiàn)轉(zhuǎn)置表格的示例代碼(行列互換),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02
vue項(xiàng)目中監(jiān)聽手機(jī)物理返回鍵的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目中監(jiān)聽手機(jī)物理返回鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

