菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首页 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue $emit() 方法


示例

使用 $emit() 方法在点击按钮时触发自定义事件到父组件。

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Click the button to trigger the custom event up to the parent component using the $emit() method.</p>
    <button v-on:click="this.$emit('custom-event')">Trigger</button>
  </div>
</template>
运行示例 »

更多示例请参见下方。


定义和用法

内置的 $emit() 方法会触发一个自定义事件,用于向上与父组件通信。

参数 描述
自定义事件名称 默认。第一个参数是使用 $emit() 方法触发的自定义事件的名称。
更多参数 可选。可以使用一个或多个参数作为载荷与自定义事件一起发送。(请参见下面的示例 1 和 2。)

emits 选项 可用于记录组件发出的内容。使用 emits 选项可以提高可读性,但它不是必需的。(请参见下面的示例 3。)

Props 用于在相反的方向通信:从父组件向下到子组件。在教程中了解更多关于 props 的信息


更多示例

示例 1

使用 $emit() 方法发送消息到父组件,使用 'message-sent' 自定义事件。

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Write something, and send the message up to the parent component using the $emit() method.</p>
    <input type="text" v-model="message" placeholder="write something..">
    <button v-on:click="send">Send</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: null
    }
  },
  methods: {
    send() {
      this.$emit('message-sent',this.message);
    }
  }
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
input {
  display: block;
  margin-bottom: 15px;
}
</style>
运行示例 »

示例 2

使用 $emit() 方法将产品名称和评分发送到父组件。

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Rate a product:</p>
    <input type="text" v-model="productName" placeholder="Product name.." ref="inpName">
    <input type="number" v-model="productRating" placeholder="Rating 1 to 10..">
    <button v-on:click="send">Register</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productName: null,
      productRating: null
    }
  },
  methods: {
    send() {
      this.$emit('message-sent',this.productName,this.productRating);
      this.productName = null;
      this.productRating = null;
      this.$refs.inpName.focus();
    }
  },
  mounted() {
    this.$refs.inpName.focus();
  }
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
input {
  display: block;
  margin-bottom: 15px;
}
</style>
运行示例 »

示例 3

使用 emits 选项使用 $emit() 方法记录组件发出的内容。这不是必需的,但可以提高可读性。

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>Click the button to trigger the custom event up to the parent component using the $emit() method.</p>
    <button v-on:click="this.$emit('custom-event')">Trigger</button>
  </div>
</template>

<script>
export default {
  emits: ['custom-event']
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
</style>
运行示例 »

相关页面

Vue 教程:Vue $emit() Method

Vue 教程:Vue Props

Vue 教程:Vue 事件

Vue 教程: Vue v-on 指令

Vue 教程:作用域样式


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持