Menu
×
   ❮   
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 'emits' 选项


示例

使用 emits 选项来声明从组件中发射的自定义事件。

export default {
  emits: ['custom-event'],
  methods: {
    notifyParent() {
      this.$emit('custom-event','Hello! ')
    }
  }
}
运行示例 »

查看下面的更多示例


定义和用法

The emits 选项用于记录组件发射的自定义事件。

The emits 选项不是必需的,这意味着组件可以在不定义 emits 选项中的情况下发射事件。

即使 emits 选项不是必需的,仍然建议使用它,以便其他程序员可以轻松地查看组件发射的内容。

emits 选项以数组形式给出时,数组只包含发射的名称作为字符串。(参见上面的示例)

emits 选项以对象形式给出时,属性名称是发射的名称,而值是验证器函数(如果有)或 'null'(如果发射没有验证器函数)。(参见下面的示例)


更多示例

示例

使用带选项的对象作为属性,以便在父组件没有提供时显示默认的食物描述。

FoodItem.vue:

<template>
	<div>
		<h2>{{ foodName }}</h2>
		<p>{{ foodDesc }}</p>
	</div>
</template>

<script>
export default {
	props: {
		foodName: {
			type: String,
			required: true
		},
		foodDesc: {
			type: String,
			required: false,
			default: 'This is the food description...'
		}
	}
};
</script>

App.vue:

<template>
  <h1>Food</h1>
  <p>Food description is not provided for 'Pizza' and 'Rice', so the default description is used.</p>
  <div id="wrapper">
    <food-item 
      food-name="Apples" 
      food-desc="Apples are a type of fruit that grow on trees."/>
    <food-item 
      food-name="Pizza"/>
    <food-item 
      food-name="Rice"/>
  </div> 
</template>

<style>
  #wrapper {
    display: flex;
    flex-wrap: wrap;
  }
  #wrapper > div {
    border: dashed black 1px;
    flex-basis: 120px;
    margin: 10px;
    padding: 10px;
    background-color: lightgreen;
  }
</style>
运行示例 »

相关页面

Vue 教程: Vue $emit() 方法

Vue 教程: Vue 属性

Vue 参考: Vue $props 对象

Vue 参考: Vue $emit() 方法


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.