国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

前端基礎(chǔ)(props emit slot 父子組件間通信)

這篇具有很好參考價值的文章主要介紹了前端基礎(chǔ)(props emit slot 父子組件間通信)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

前言:如何實(shí)現(xiàn)組件的靈活使用,今天學(xué)習(xí)組件封裝用到的props、slot和emit。

目錄

props

子組件

父組件

示例代碼

slot

示例代碼

作用域插槽

emit

示例代碼


props

需要實(shí)現(xiàn)在其他組件中使用同一個子組件。

子組件

子組件(所謂子組件,就是封裝好的組件,供其他組件使用)

子組件定義了sonName

<div>我是MRJJ_9{{sonName}}</div>

defineProps(['sonName'])

或 const props=defineProps(['sonName'])

props是只讀的,盡量不要去修改

定義多個

const props=defineProps(['sonName1','sonName2'])

但通常使用數(shù)組定義

const props = defineProps({
? sonName1: Object,
? sonName: Number,})

父組件

父組件(所謂父組件,就是引用封裝好的其他子組件)

<Mrjj-Counter :sonName="sonName"></Mrjj-Counter>

let sonName=ref("引用子組件")

示例代碼

子組件設(shè)置

<template>
  <div>我是MRJJ_9的第一個屬性,類型為字符串,內(nèi)容是:{{ sonName1 }},第二個屬性,類型是數(shù)字,數(shù)值為:{{ sonName2 }}</div>
</template>
<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number,
})
console.log("屬性1",props.sonName1)
console.log("屬性2",props.sonName2)
</script>
<style lang="scss" scoped>
</style>

父組件設(shè)置

<template>
  <mrjj-son :sonName1="sonName1" :sonName2="sonName2"></mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

?前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue

要注意不能去修改里面的值

前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue

slot

需要實(shí)現(xiàn)在其他組件中使用同一個組件(子組件),但組件樣式的有所區(qū)別

這就需要用到插槽:slot,其作用是傳參時可以帶上HTML結(jié)構(gòu)

子組件帶上slot

{{ sonName }}<slot></slot>

父組件將需要傳遞的內(nèi)容寫到子組件標(biāo)簽里

<mrjj-son><strong>{{sonName }}</strong></mrjj-son>

具名插槽,給插槽命名

有多個值時

子組件加上name

父組件,用v-slot:插槽名(或#插槽名)

示例代碼

子組件設(shè)置

<template>
  <div>
    {{ sonName1 }}
    <slot name="mrjj1"></slot>
    {{ sonName2 }}
    <slot name="mrjj2"></slot>
  </div>
</template>
<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number
})
</script>
<style lang="scss" scoped>
</style>

父組件設(shè)置

<template>
  <mrjj-son>
    <template #mrjj1
      ><strong>{{ sonName1 }}</strong></template
    >
    <template #mrjj2
      ><i>{{ sonName2 }}</i></template
    >
  </mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
</script>
<style lang="scss" scoped>
</style>

效果展示

前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue

作用域插槽

子組件

<template>
  <div>
    {{ sonName3 }}
    <slot name="mrjj3" :times="count" :mrjj1="name"></slot>
  </div>
</template>
<script setup>
import { ref } from 'vue'

const props = defineProps({
  sonName3: String
})
let count = ref(0)
let name = ref('計(jì)數(shù)器')
</script>
<style lang="scss" scoped>
</style>

父組件?

<template #mrjj3="{ times }"

<template>
  <mrjj-son>
    <template #mrjj3="{ times }"
      ><i>{{ sonName3 }}</i>
      <Times :times="times"></Times>
    </template>
  </mrjj-son>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import Times from '@/components/Times.vue'
import { ref } from 'vue'
let sonName3 = ref('')
</script>
<style lang="scss" scoped>
</style>

引用的Time.vue文件

<template>
  <h1>顯示Mrjj{{ times }}</h1>
</template>
<script setup>
defineProps(['times'])
</script>

前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue

emit

需求:增加一個關(guān)閉、打開的組件功能

用到emit,emit干了什么事情呢?在子組件中觸發(fā)一個事件,在父組件中進(jìn)行監(jiān)聽。

示例代碼

子組件定義一個自定義事件

<template>
  <div>
    {{ sonName1 }}
    <slot name="mrjj1"></slot>
    {{ sonName2 }}
    <slot name="mrjj2"></slot>
    <button @click="closeSon">點(diǎn)我關(guān)閉</button>
  </div>
</template>


<script setup>
const props = defineProps({
  sonName1: String,
  sonName2: Number
})
const emit = defineEmits(['closeMrjj'])

function closeSon() {
  console.log('關(guān)閉按鈕被點(diǎn)擊了!')
  emit('closeMrjj')
}
</script>
<style lang="scss" scoped>
</style>

父組件綁定事件

<template>
  <mrjj-son @closeMrjj="closeMrjj" v-if="isClose">
    <template #mrjj1
      ><strong>{{ sonName1 }}</strong></template
    >
    <template #mrjj2
      ><i>{{ sonName2 }}</i></template
    >
  </mrjj-son>
  <button v-else @click="($event) => (isClose = true)">點(diǎn)我打開</button>
</template>
<script setup>
import MrjjSon from '@/components/MrjjSon.vue'
import { ref } from 'vue'
let sonName1 = ref('hello,world!!!')
let sonName2 = ref(999)
let isClose = ref(false)
function closeMrjj() {
  isClose.value = false
}
</script>
<style lang="scss" scoped>
</style>

效果展示

點(diǎn)我關(guān)閉按鈕,點(diǎn)擊后,調(diào)用了closeSon函數(shù),可以看到console輸出的信息。

點(diǎn)擊展開后,也可以展示出內(nèi)容。

前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue前端基礎(chǔ)(props emit slot 父子組件間通信),前端技術(shù),前端,開發(fā)語言,vue文章來源地址http://www.zghlxwxcb.cn/news/detail-680271.html

到了這里,關(guān)于前端基礎(chǔ)(props emit slot 父子組件間通信)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Web前端 ---- 【Vue】(組件)父子組件之間的通信一文帶你了解

    Web前端 ---- 【Vue】(組件)父子組件之間的通信一文帶你了解

    目錄 前言 父組件傳子組件 ---- props 給要傳遞數(shù)據(jù)的子組件綁定要傳過去的屬性及屬性值 在子組件中使用props配置項(xiàng)接收 props配置項(xiàng) 子組件傳父組件 ---- 組件的自定義事件 子組件向父組件傳遞數(shù)據(jù) 通過代碼來綁定自定義事件 本文將介紹在Vue中父子組件如何進(jìn)行通信 這里先介

    2024年02月05日
    瀏覽(631)
  • Vue組件的嵌套關(guān)系;父組件傳遞子組件props;子組件傳遞給父組件$emit;自定義事件;案例

    Vue組件的嵌套關(guān)系;父組件傳遞子組件props;子組件傳遞給父組件$emit;自定義事件;案例

    前面將所有的邏輯放到一個App.vue中: 在之前的案例中,只是創(chuàng)建了一個組件App; 如果一個應(yīng)用程序?qū)⑺械倪壿嫸挤旁谝粋€組件中,那么這個組件就會變成非常的臃 腫和難以維護(hù); 所以組件化的核心思想應(yīng)該是對組件進(jìn)行拆分,拆分成一個個小的組件; 再將這些組件組合

    2024年02月13日
    瀏覽(23)
  • 父組件明明使用了v-model,子組件竟然可以不用定義props和emit拋出事件,快來看看吧

    父組件明明使用了v-model,子組件竟然可以不用定義props和emit拋出事件,快來看看吧

    vue3.4增加了 defineModel 宏函數(shù),在子組件內(nèi)修改了 defineModel 的返回值,父組件上 v-model 綁定的變量就會被更新。大家都知道 v-model 是 :modelValue 和 @update:modelValue 的語法糖,但是你知道為什么我們在子組件內(nèi)沒有寫任何關(guān)于 props 的定義和 emit 事件觸發(fā)的代碼嗎?還有在 template

    2024年04月08日
    瀏覽(25)
  • Vue:父子組件傳值( props、sync、v-model )

    子組件通過$emit方法,通過自定義事件的方式將自身的值傳遞給父組件 在有些情況下,我們可能需要對一個 prop 進(jìn)行“雙向綁定”。不幸的是,真正的雙向綁定會帶來維護(hù)上的問題,因?yàn)樽咏M件可以變更父組件,且在父組件和子組件兩側(cè)都沒有明顯的變更來源。 所以推薦以

    2023年04月08日
    瀏覽(25)
  • vue3組合式api <script setup> props 父子組件的寫法

    父組件傳入子組個的變量, 子組件是無法直接修改的, 只能通過 emit的方式, 讓父組件修改, 之后子組件更新 父組件的寫法沒有變, 子組件的寫法就有很大的變化了

    2024年02月10日
    瀏覽(26)
  • VUE-組件間通信(二)$emit

    VUE-組件間通信(二)$emit

    $emit 1、單向綁定 子組件向父組件傳值 2、使用示例 父組件 子組件 效果

    2024年03月20日
    瀏覽(19)
  • Vue組件通信——父子組件通信的四種方法

    Vue組件通信——父子組件通信的四種方法

    全局引入 在main.js文件中引入并注冊 之后就可以全局使用組件了 局部引入 在父組件中引入 之后就可以在父組件中使用組件了 在子組件 prop 中可以注冊一些自定義組件屬性,父組件調(diào)用子組件時可以向 prop 中的自定義屬性傳值。 子組件代碼: 父組件代碼 prop 也可以通過 v-

    2023年04月16日
    瀏覽(101)
  • react 組件之間的通信(父子組件)

    React中 組件內(nèi)調(diào)用其他組件不需要進(jìn)行 類似于vue 聲明組件(components) React 組件內(nèi)調(diào)用其他組件 直接將組件導(dǎo)入 放置在對應(yīng)的JSX 代碼中 父子組件通信(傳統(tǒng)): 1、父組件-子組件 ?通過屬性傳遞 2、子組件-父組件 ?父組件通過將自身的函數(shù)對象傳遞給子組件, 子組件執(zhí)行父組件

    2024年02月08日
    瀏覽(93)
  • Vue中父子組件通信

    Vue中父子組件通信

    聚沙成塔·每天進(jìn)步一點(diǎn)點(diǎn) Vue學(xué)習(xí)之旅的奇妙世界 歡迎大家來到 Vue 技能樹參考資料專欄!創(chuàng)建這個專欄的初衷是為了幫助大家更好地應(yīng)對 Vue.js 技能樹的學(xué)習(xí)。每篇文章都致力于提供清晰、深入的參考資料,讓你能夠更輕松、更自信地理解和掌握 Vue.js 的核心概念和技術(shù)。訂

    2024年01月21日
    瀏覽(91)
  • VUE--組件通信(非父子)

    VUE--組件通信(非父子)

    一、非父子通信? ---? event bus 事件總線 ? ? ? ? 作用:非父子組件之間進(jìn)行 簡易的消息傳遞 ? ? ? ? 步驟:? ??????????????? ? 1、創(chuàng)建一個都能訪問到的事件總線(空vue實(shí)例)--- utils/EventBus.js ? ? ? ? ? ? ? ? ? 2、?接收方(A組件),監(jiān)聽Bus實(shí)例的事件 ? ? ? ?

    2024年01月19日
    瀏覽(93)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包