2023-08-13|閱讀時間 ‧ 約 2 分鐘

Vue 3.2 defineEmits 用法

  • 父元件

傳遞方法使用@

<template>
   ...
   <Login @modalClose="modalClose"/>
...
</template>

<script setup>

    const _modal = ref();
   
function modalShow(modalId){
        let modal = document.getElementById(modalId);
        modal.style.display = "block";
        _modal.value = modal;
    }


    function modalClose(){
        _modal.value.style.display = "none";
    }
   

</script>


  • 子元件 Login.vue

按下Login按鈕後,call父元件的modalClose方法。


<template>
   ...
<input type="button" value="Login" @click="login()">
...
</template>

<script setup>

const emit = defineEmits(["modalClose"]);  //接收父組件傳來的方法

...

function login(){
   emit("modalClose");
}

</script>



本筆記參考:

  1. https://juejin.cn/post/7046940339621330974
分享至
成為作者繼續創作的動力吧!
小弟是一位軟體工程師,樂於幫助他人,撰寫技術文章除了幫助自己複習以外,也希望可以幫助到他人,若文章內容有誤,還請大大不吝給予指教!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言