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

这是官方的文字介绍。emits重要用于组件之间的通信,触发自定义事件,传递参数。

下面演示一个子组件把事件传递到父组件,组件间通信的例子。

<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的显示与隐藏,点击按钮,触发clickButton函数,父组件调用,触发自定义事件close-modal,而close-modal在父组件中绑定了onModalClose函数,实现了对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">关闭模态框</button>
   </div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  props:{

    isOpen: {
      type: Boolean,
      required: true
    },
  },
  // emits 写自定义事件  作用 比较清晰知道该组件有那些自定义事件
  emits: {
    // 无需验证写法
    'close-model': null,

    // 这种写法  自定义函数  可以在运行时验证参数是否正确
    '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>

总结

到此这篇关于vue3.x中emits基本用法的文章就介绍到这了,更多相关vue3.x中emits用法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Vue3中emits与attrs的区别分析

    目录 结论 实践分析 扩展 总结 结论 当在父组件自定义事件,若没有在子组件中声明时,将自动绑定在父组件的$attrs上:而当在子组件声明时,则不会在父组件的$attrs上出现 实践分析 为了验证emits和attrs的区别,我们构造这样的组件结构 <div> <com-one-vue/> </div> <div> <com-one-vue/> </div> 其具体的Vue文件及代码为(注意,以下语法均采用 setup语法糖 ): A

  • vue3.0语法糖内的defineProps及defineEmits解析

    目录 语法糖内的defineProps及defineEmits 1.defineProps 2.defineEmits vue语法糖的说明 语法糖内的defineProps及defineEmits 1.defineProps 获取组件传值 <div :style="fontstyle"> <div class="rate" @mouseout="mouseOut"> <span @mouseover="mo

  • vue3.x中emits的基本用法实例

    这是官方的文字介绍.emits重要用于组件之间的通信,触发自定义事件,传递参数. 下面演示一个子组件把事件传递到父组件,组件间通信的例子. <template> <teleport to="#modal"> <div id="center" v-if="isOpen"> <h2> <slot>this is a modal</slot> </h2> <but

  • vue3 element的Form表单用法实例

    目录 引言 设计目标 配置化 参数简单 自由度 实现过程 表单项的格式设计 v-bind的妙用 computed的妙用:实现v-model useAttrs的妙用 表单验证 上传文件 代码总结 到底应不应该使用json 需不需要v-model 性能问题 引言 最近在做一系列后台管理系统,其中用的最多的就是表单和表格了.这里讲一下我最近对表单封装的思考. 以下是我的设计思路以及具体实现,我使用的是vue3+element-plus,因此这个组件也是以这两个库为基础. 已上传npm www.npmj

  • Sql Server中Substring函数的用法实例解析

    SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分.这个函数的名称在不同的资料库中不完全一样: MySQL: SUBSTR( ), SUBSTRING( ) Oracle: SUBSTR( ) SQL Server: SUBSTRING( ) SQL 中的 substring 函数是用来截取一个栏位资料中的其中一部分. 例如,我们需要将字符串'abdcsef'中的'abd'给提取出来,则可用substring 来实现: select substring('abdcsef'

  • java中静态导入机制用法实例详解

    java中静态导入机制用法实例详解 这里主要讲解了如何使用Java中静态机制的用法,这里提供了简单实例大家可以参考下. 静态常量类 在java开发中,我们会经常用到一些静态常量用于状态判断等操作.为了能够在多个地方复用这些常量,通常每个模块都会加一个常量类,举个简单的列子: import com.sky.OrderMouleConsstants; /** * Created by gantianxing on 2017/4/21. */ public class Test { public vo

  • Python中np.linalg.norm()用法实例总结

    目录 前言 用法 总结 前言 np.linalg.norm()用于求范数,linalg本意为linear(线性) + algebra(代数),norm则表示范数. 用法 np.linalg.norm(x, ord=None, axis=None, keepdims=False) 1.x: 表示矩阵(一维数据也是可以的~) 2.ord: 表示范数类型 向量的范数: 矩阵的向量: ord=1:表示求列和的最大值 ord=2:|λE-ATA|=0,求特征值,然后求最大特征值得算术平方根 ord=∞:表

  • JQuery中基础过滤选择器用法实例分析

    本文实例讲述了JQuery中基础过滤选择器用法.分享给大家供大家参考.具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <he

  • JAVA中的final关键字用法实例详解

    本文实例讲述了JAVA中的final关键字用法.分享给大家供大家参考,具体如下: 根据上下文环境,java的关键字final也存在着细微的区别,但通常指的是"这是无法改变的."不想改变的理由有两种:一种是效率,另一种是设计.由于两个原因相差很远,所以关键子final可能被误用. 接下来介绍一下使用到final的三中情况:数据,方法,类 final数据 许多编程语言都有某种方法,来向编译器告知一块数据是恒定不变的.有时数据的恒定不变是很有用的,例如: 1. 一个编译时恒定不变的常量 2.

  • ASP.NET中repeater控件用法实例

    本文实例讲述了ASP.NET中repeater控件用法.分享给大家供大家参考.具体实现方法如下: repeater绑定数据: 复制代码 代码如下: protected void Page_Load(object sender, EventArgs e) {         if(!IsPostBack)             BindStudent(); } private void BindStudent() {         string str = ConfigurationManag

  • js中setTimeout()与clearTimeout()用法实例浅析

    本文实例分析了js中setTimeout()与clearTimeout()用法.分享给大家供大家参考.具体分析如下: setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. clearTimeout() 方法可取消由 setTimeout() 方法设置的 timeout. <input type = text id = aaa > <input type = button value = stop id = bb onclick = bb()> <scrip

  • ASP.NET中Application全局对象用法实例浅析

    本文实例讲述了ASP.NET中Application全局对象用法.分享给大家供大家参考.具体如下: Application是应用全局对象,被全体共享.无论通过哪个页面操作Application,另一个页面都可以读取Application信息. 由于Application是共享的,操作之前先Lock,操作完成后UnLock. 在一个页面设置数据: Application.Lock(); Application.Set("address", "上海"); Applica

随机推荐