最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3.x中emits的基本用法實例

 更新時間: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的顯示與隱藏,點擊按鈕,觸發(fā)clickButton函數(shù),父組件調(diào)用,觸發(fā)自定義事件close-modal,而close-modal在父組件中綁定了onModalClose函數(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: {
    // 無需驗證寫法
    'close-model': null,

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

    // 這種寫法來校驗
    context.emit('close-modals',{
      // 如果驗證失敗會有一個警告
      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實現(xiàn)Google第三方登錄的示例代碼

    Vue實現(xiàn)Google第三方登錄的示例代碼

    本文記錄作者在vue項目中使用到Google第三方登錄,查詢到的資料文檔也不詳細,故此把自己所遇到的坑及問題詳細的記錄下來。
    2021-07-07
  • vue使用async/await來實現(xiàn)同步和異步的案例分享

    vue使用async/await來實現(xiàn)同步和異步的案例分享

    近期項目中大量使用async,從服務(wù)器獲取數(shù)據(jù),解決一些并發(fā)傳參問題,代碼很簡單,在此也看了一些博客,現(xiàn)在async/await已經(jīng)大范圍讓使用,所以本文給大家介紹一下vue使用async/await來實現(xiàn)同步和異步的案例,需要的朋友可以參考下
    2024-01-01
  • vue后臺系統(tǒng)管理項目之角色權(quán)限分配管理功能(示例詳解)

    vue后臺系統(tǒng)管理項目之角色權(quán)限分配管理功能(示例詳解)

    這篇文章主要介紹了vue后臺系統(tǒng)管理項目-角色權(quán)限分配管理功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • vue項目中v-model父子組件通信的實現(xiàn)詳解

    vue項目中v-model父子組件通信的實現(xiàn)詳解

    vue.js,是一個構(gòu)建數(shù)據(jù)驅(qū)動的 web 界面的庫。Vue.js 的目標是通過盡可能簡單的 API 實現(xiàn)響應的數(shù)據(jù)綁定和組合的視圖組件。下面這篇文章主要給大家介紹了關(guān)于vue項目中v-model父子組件通信實現(xiàn)的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • 詳解Vue.js v-for不支持IE9的解決方法

    詳解Vue.js v-for不支持IE9的解決方法

    這篇文章主要介紹了詳解Vue.js v-for不支持IE9的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • vue實現(xiàn)條件疊加搜索的解決方法

    vue實現(xiàn)條件疊加搜索的解決方法

    這篇文章主要為大家詳細介紹了vue實現(xiàn)條件疊加搜索的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Vue項目中v-model和sync的區(qū)別及使用場景分析

    Vue項目中v-model和sync的區(qū)別及使用場景分析

    在Vue項目中,v-model和.sync是實現(xiàn)父子組件雙向綁定的兩種方式,v-model主要用于表單元素和子組件的雙向綁定,通過modelValue和update:modelValue實現(xiàn),.sync修飾符則用于同步prop值,適合在子組件內(nèi)更新父組件prop值的場景,通過update:propName事件實現(xiàn)
    2024-11-11
  • vue利用vant組件實現(xiàn)輪播圖效果

    vue利用vant組件實現(xiàn)輪播圖效果

    vant組件適用于移動端項目,目前項目開源,是市面上做的比較好的開源項目,功能比較強大,本文小編就來為大家介紹一下如何利用vant實現(xiàn)輪播圖效果吧
    2023-10-10
  • vue項目打包以及優(yōu)化的實現(xiàn)步驟

    vue項目打包以及優(yōu)化的實現(xiàn)步驟

    項目完成,我們會將項目進行上線,為了提升性能,我們往往會進行一些優(yōu)化處理,本文主要介紹了vue項目打包以及優(yōu)化的實現(xiàn)步驟,感興趣的可以了解一下
    2021-07-07
  • vue指令v-html使用過濾器filters功能實例

    vue指令v-html使用過濾器filters功能實例

    在本篇文章里我們給大家整理的是關(guān)于vue指令v-html使用過濾器filters功能的實例內(nèi)容,需要的朋友們學習下。
    2019-10-10

最新評論

手机| 金塔县| 永定县| 永顺县| 本溪市| 中江县| 镇宁| 玉环县| 高碑店市| 乌兰浩特市| 霍林郭勒市| 贞丰县| 错那县| 谢通门县| 微山县| 石泉县| 杭锦后旗| 东光县| 长垣县| 西乡县| 丰都县| 常熟市| 株洲县| 灯塔市| 榆社县| 乌海市| 尚义县| 登封市| 同仁县| 罗江县| 黔南| 韩城市| 泸溪县| 张北县| 黄大仙区| 陵水| 义马市| 明水县| 巴中市| 郓城县| 大渡口区|