欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3.x中emits的基本用法實(shí)例

 更新時間:2022年07月18日 08:57:45   作者:丁七歲  
emits是Vue3新增的選項,emits主要作用在子組件中,用于接收在父組件中綁定的方法,這篇文章主要給大家介紹了關(guān)于vue3.x中emits的基本用法,需要的朋友可以參考下

這是官方的文字介紹。emits重要用于組件之間的通信,觸發(fā)自定義事件,傳遞參數(shù)。

下面演示一個子組件把事件傳遞到父組件,組件間通信的例子。

<template>
  <teleport to="#modal">
    <div id="center" v-if="isOpen">
      <h2>
        <slot>this is a modal</slot>
      </h2>
      <button @click="clickButton">close</button>
    </div>
  </teleport>
</template>
<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  props: {
    isOpen: Boolean,
  },
  emits: {
    'close-modal': null,
  },
  setup(props, context) {
    const clickButton = () => {
      context.emit('close-modal');
    };
    return {
      clickButton,
    };
  },
});
</script>

<style scoped>
#center {
  width: 200px;
  height: 200px;
  border: 2px solid black;
  background: white;
  position: fixed;
  left: 50%;
  top: 50%;
  margin-left: -100px;
  margin-top: -100px;
}
</style>

isOpen用來表示Modal的顯示與隱藏,點(diǎn)擊按鈕,觸發(fā)clickButton函數(shù),父組件調(diào)用,觸發(fā)自定義事件close-modal,而close-modal在父組件中綁定了onModalClose函數(shù),實(shí)現(xiàn)了對Modal的隱藏。

<template>
  <div id="main">
    <modal :isOpen="modalIsOpen" @close-modal="onModalClose">my modal</modal>
    <button @click="onModalOpen">Open Modal</button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import Modal from './components/Modal.vue';

export default defineComponent({
  components: { Modal },
  name: 'App',
  setup(){
    const modalIsOpen = ref(false)
    const onModalOpen = ()=>{
      modalIsOpen.value = true
    }
    const onModalClose = ()=>{
      modalIsOpen.value = false
    }
    return{
      onModalOpen,
      onModalClose,
      modalIsOpen
    }
  }
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
button {
  font-size: 2rem;
}
</style>

emits的文檔

附:vue3自定義組件中的emits使用介紹

<template>
  <!-- teleport的使用  to屬性渲染到id為 teleport-test 的元素身上   在index.html中-->
  
   <div id="center" v-if="isOpen">
     <slot>插槽</slot>
     <button @click="buttonClick">關(guān)閉模態(tài)框</button>
   </div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  props:{

    isOpen: {
      type: Boolean,
      required: true
    },
  },
  // emits 寫自定義事件  作用 比較清晰知道該組件有那些自定義事件
  emits: {
    // 無需驗(yàn)證寫法
    'close-model': null,

    // 這種寫法  自定義函數(shù)  可以在運(yùn)行時驗(yàn)證參數(shù)是否正確
    'close-modals': (payload: any) => {
      return payload.type === 'close'
    }
  },
  setup(props,context) {
    const buttonClick = () => {
      context.emit('close-model')
    }

    // 這種寫法來校驗(yàn)
    context.emit('close-modals',{
      // 如果驗(yàn)證失敗會有一個警告
      type: 'close'
      // type: 'sss'
    })
    return {
      buttonClick
    }
  }
})

</script>

<style>
#center{
  width: 600px;
  height: 300px;
  border: 1px solid #999;
  background-color: #fff;
  position: fixed;
  left: 50%;
  top: 50%;
  margin-left: -300px;
  margin-top: -150px;
}
</style>

總結(jié)

到此這篇關(guān)于vue3.x中emits基本用法的文章就介紹到這了,更多相關(guān)vue3.x中emits用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue項目使用px2rem方法示例詳解

    Vue項目使用px2rem方法示例詳解

    這篇文章主要為大家介紹了Vue項目使用px2rem的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例

    vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例

    下面小編就為大家分享一篇vue組件傳遞對象中實(shí)現(xiàn)單向綁定的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 解決vue項目獲取dom元素寬高總是不準(zhǔn)確問題

    解決vue項目獲取dom元素寬高總是不準(zhǔn)確問題

    這篇文章主要介紹了解決vue項目獲取dom元素寬高總是不準(zhǔn)確問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue數(shù)據(jù)雙向綁定原理解析(get & set)

    vue數(shù)據(jù)雙向綁定原理解析(get & set)

    這篇文章主要為大家詳細(xì)解析了vue.js數(shù)據(jù)雙向綁定原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue前端開發(fā)keepAlive使用詳解

    vue前端開發(fā)keepAlive使用詳解

    在開發(fā)中經(jīng)常有從列表跳到詳情頁,然后返回詳情頁的時候需要緩存列表頁的狀態(tài)(比如滾動位置信息),這個時候就需要保存狀態(tài),要緩存狀態(tài)
    2021-10-10
  • 一文帶你了解Vue3中toRef和toRefs的用法

    一文帶你了解Vue3中toRef和toRefs的用法

    Vue3中toRef、toRefs都是與響應(yīng)式數(shù)據(jù)相關(guān)的,本文主要來給大家講解一下Vue3中的toRef和toRefs的使用,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • Vue+Websocket簡單實(shí)現(xiàn)聊天功能

    Vue+Websocket簡單實(shí)現(xiàn)聊天功能

    這篇文章主要為大家詳細(xì)介紹了Vue+Websocket簡單實(shí)現(xiàn)聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • element使用自定義icon圖標(biāo)的詳細(xì)步驟

    element使用自定義icon圖標(biāo)的詳細(xì)步驟

    前端經(jīng)常會用到UI提供的各種圖表,推薦阿里的圖標(biāo)庫,下面這篇文章主要給大家介紹了關(guān)于element使用自定義icon圖標(biāo)的詳細(xì)步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • vue父子組件slot插槽的使用

    vue父子組件slot插槽的使用

    這篇文章主要介紹了vue父子組件slot插槽的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue router中mode history、base的作用說明

    vue router中mode history、base的作用說明

    這篇文章主要介紹了vue router中mode history、base的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論