1
1
# 事件处理
2
2
3
- Vue 组件通过 props 互相交互,并通过调用 ` $emit ` 触发事件 。在本指南中,我们将探讨如何使用 ` emitted() ` 函数验证事件是否正确触发 。
3
+ Vue 组件通过 props 和调用 ` $emit ` 触发事件来进行交互 。在本指南中,我们将介绍如何使用 ` emitted() ` 函数来验证事件是否正确触发 。
4
4
5
- 本文也有[ 短视频] ( https://www.youtube.com/watch?v=U_j-nDur4oU&list=PLC2LZCNWKL9ahK1IoODqYxKu5aA9T5IOA&index=14 ) 观看 。
5
+ 本文也有[ 短视频] ( https://www.youtube.com/watch?v=U_j-nDur4oU&list=PLC2LZCNWKL9ahK1IoODqYxKu5aA9T5IOA&index=14 ) 可供观看 。
6
6
7
7
## 计数器组件
8
8
@@ -25,11 +25,11 @@ const Counter = {
25
25
}
26
26
```
27
27
28
- 为了全面测试这个组件,我们需要验证是否发出了带有最新 ` count ` 值的 ` increment ` 事件。
28
+ 为了全面测试这个组件,我们需要验证是否触发了带有最新 ` count ` 值的 ` increment ` 事件。
29
29
30
30
## 断言触发的事件
31
31
32
- 为此,我们将依赖 ` emitted() ` 方法。它** 返回一个对象,包含组件发出的所有事件 ** ,其参数以数组的形式呈现。让我们看看它是如何工作的:
32
+ 为此,我们将依赖 ` emitted() ` 方法。它** 返回一个对象,包含组件触发的所有事件 ** ,其参数以数组的形式呈现。让我们看看它是如何工作的:
33
33
34
34
``` js
35
35
test (' emits an event when clicked' , () => {
@@ -44,15 +44,15 @@ test('emits an event when clicked', () => {
44
44
45
45
> 如果你之前没有见过 ` trigger() ` ,不要担心。它用于模拟用户交互。你可以在[ 测试表单] ( ./forms ) 中了解更多。
46
46
47
- 首先要注意的是,` emitted() ` 返回一个对象,其中每个键对应一个已触发的事件 。在这个例子中是 ` increment ` 。
47
+ 首先要注意的是,` emitted() ` 返回一个对象,其中每个键都匹配一个已触发的事件 。在这个例子中是 ` increment ` 。
48
48
49
49
这个测试应该通过。我们确保发出了具有适当名称的事件。
50
50
51
51
## 断言事件的参数
52
52
53
- 干得漂亮 ,但我们可以做得更好!我们需要检查在调用 ` this.$emit('increment', this.count) ` 时是否发出了正确的参数。
53
+ 这很好 ,但我们可以做得更好!我们需要检查在调用 ` this.$emit('increment', this.count) ` 时是否发出了正确的参数。
54
54
55
- 我们的下一步是断言事件包含 ` count ` 值。我们通过向 ` emitted() ` 传递参数来实现这一点 。
55
+ 我们的下一步是断言事件包含 ` count ` 值。我们通过将参数传递给 ` emitted() ` 来实现这一点 。
56
56
57
57
``` js {9}
58
58
test (' emits an event with count when clicked' , () => {
@@ -61,11 +61,10 @@ test('emits an event with count when clicked', () => {
61
61
wrapper .find (' button' ).trigger (' click' )
62
62
wrapper .find (' button' ).trigger (' click' )
63
63
64
- // `emitted()` 接受一个参数。它返回一个数组,包含
65
- // 所有 `this.$emit('increment')` 的发生情况。
64
+ // `emitted()` 接受一个参数。它返回一个包含所有 `this.$emit('increment')` 发生情况的数组。
66
65
const incrementEvent = wrapper .emitted (' increment' )
67
66
68
- // 我们“点击”了两次,因此 `increment` 的数组应该有两个值。
67
+ // 我们“点击”了两次,所以 `increment` 的数组应该有两个值。
69
68
expect (incrementEvent).toHaveLength (2 )
70
69
71
70
// 断言第一次点击的结果。
@@ -77,7 +76,7 @@ test('emits an event with count when clicked', () => {
77
76
})
78
77
```
79
78
80
- 让我们回顾一下并分解 ` emitted() ` 的输出。每个键包含测试期间发出的不同值 :
79
+ 让我们回顾一下并分解 ` emitted() ` 的输出。每个键都包含测试期间触发的不同值 :
81
80
82
81
``` js
83
82
// console.log(wrapper.emitted('increment'))
@@ -89,7 +88,7 @@ test('emits an event with count when clicked', () => {
89
88
90
89
## 断言复杂事件
91
90
92
- 假设我们的 ` <Counter> ` 组件现在需要发出一个包含附加信息的对象 。例如,我们需要告诉任何监听 ` @increment ` 事件的父组件 ` count ` 是偶数还是奇数:
91
+ 假设我们的 ` <Counter> ` 组件现在需要触发一个包含附加信息的对象 。例如,我们需要告诉任何监听 ` @increment ` 事件的父组件 ` count ` 是偶数还是奇数:
93
92
94
93
``` js {12-15}
95
94
const Counter = {
@@ -112,7 +111,7 @@ const Counter = {
112
111
}
113
112
```
114
113
115
- 和之前一样 ,我们需要在 ` <button> ` 元素上触发 ` click ` 事件。然后,我们使用 ` emitted('increment') ` 确保发出了正确的值 。
114
+ 正如我们之前所做的那样 ,我们需要在 ` <button> ` 元素上触发 ` click ` 事件。然后,我们使用 ` emitted('increment') ` 来确保触发了正确的值 。
116
115
117
116
``` js
118
117
test (' emits an event with count when clicked' , () => {
@@ -124,8 +123,7 @@ test('emits an event with count when clicked', () => {
124
123
// 我们“点击”了两次,因此 `increment` 的数组应该有两个值。
125
124
expect (wrapper .emitted (' increment' )).toHaveLength (2 )
126
125
127
- // 然后,我们可以确保 `wrapper.emitted('increment')` 的每个元素
128
- // 包含一个带有预期对象的数组。
126
+ // 然后,我们可以确保 `wrapper.emitted('increment')` 的每个元素包含一个带有预期对象的数组。
129
127
expect (wrapper .emitted (' increment' )[0 ]).toEqual ([
130
128
{
131
129
count: 1 ,
@@ -150,6 +148,6 @@ test('emits an event with count when clicked', () => {
150
148
151
149
## 结论
152
150
153
- - 使用 ` emitted() ` 访问从 Vue 组件发出的事件 。
154
- - ` emitted(eventName) ` 返回一个数组,其中每个元素代表一个已发出的事件 。
155
- - 参数存储在 ` emitted(eventName)[index] ` 中,按发出顺序以数组形式呈现 。
151
+ - 使用 ` emitted() ` 来访问从 Vue 组件触发的事件 。
152
+ - ` emitted(eventName) ` 返回一个数组,其中每个元素代表一个已触发的事件 。
153
+ - 参数存储在 ` emitted(eventName)[index] ` 中,按触发顺序以数组形式呈现 。
0 commit comments