Skip to content

Commit ba185e9

Browse files
authored
docs(zh-cn): review event-handing.md (#2572)
1 parent 7ecc2f3 commit ba185e9

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

docs/zh/guide/essentials/event-handling.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# 事件处理
22

3-
Vue 组件通过 props 互相交互,并通过调用 `$emit` 触发事件。在本指南中,我们将探讨如何使用 `emitted()` 函数验证事件是否正确触发
3+
Vue 组件通过 props 和调用 `$emit` 触发事件来进行交互。在本指南中,我们将介绍如何使用 `emitted()` 函数来验证事件是否正确触发
44

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)可供观看
66

77
## 计数器组件
88

@@ -25,11 +25,11 @@ const Counter = {
2525
}
2626
```
2727

28-
为了全面测试这个组件,我们需要验证是否发出了带有最新 `count` 值的 `increment` 事件。
28+
为了全面测试这个组件,我们需要验证是否触发了带有最新 `count` 值的 `increment` 事件。
2929

3030
## 断言触发的事件
3131

32-
为此,我们将依赖 `emitted()` 方法。它**返回一个对象,包含组件发出的所有事件**,其参数以数组的形式呈现。让我们看看它是如何工作的:
32+
为此,我们将依赖 `emitted()` 方法。它**返回一个对象,包含组件触发的所有事件**,其参数以数组的形式呈现。让我们看看它是如何工作的:
3333

3434
```js
3535
test('emits an event when clicked', () => {
@@ -44,15 +44,15 @@ test('emits an event when clicked', () => {
4444

4545
> 如果你之前没有见过 `trigger()`,不要担心。它用于模拟用户交互。你可以在[测试表单](./forms)中了解更多。
4646
47-
首先要注意的是,`emitted()` 返回一个对象,其中每个键对应一个已触发的事件。在这个例子中是 `increment`
47+
首先要注意的是,`emitted()` 返回一个对象,其中每个键都匹配一个已触发的事件。在这个例子中是 `increment`
4848

4949
这个测试应该通过。我们确保发出了具有适当名称的事件。
5050

5151
## 断言事件的参数
5252

53-
干得漂亮,但我们可以做得更好!我们需要检查在调用 `this.$emit('increment', this.count)` 时是否发出了正确的参数。
53+
这很好,但我们可以做得更好!我们需要检查在调用 `this.$emit('increment', this.count)` 时是否发出了正确的参数。
5454

55-
我们的下一步是断言事件包含 `count` 值。我们通过向 `emitted()` 传递参数来实现这一点
55+
我们的下一步是断言事件包含 `count` 值。我们通过将参数传递给 `emitted()` 来实现这一点
5656

5757
```js {9}
5858
test('emits an event with count when clicked', () => {
@@ -61,11 +61,10 @@ test('emits an event with count when clicked', () => {
6161
wrapper.find('button').trigger('click')
6262
wrapper.find('button').trigger('click')
6363

64-
// `emitted()` 接受一个参数。它返回一个数组,包含
65-
// 所有 `this.$emit('increment')` 的发生情况。
64+
// `emitted()` 接受一个参数。它返回一个包含所有 `this.$emit('increment')` 发生情况的数组。
6665
const incrementEvent = wrapper.emitted('increment')
6766

68-
// 我们“点击”了两次,因此 `increment` 的数组应该有两个值。
67+
// 我们“点击”了两次,所以 `increment` 的数组应该有两个值。
6968
expect(incrementEvent).toHaveLength(2)
7069

7170
// 断言第一次点击的结果。
@@ -77,7 +76,7 @@ test('emits an event with count when clicked', () => {
7776
})
7877
```
7978

80-
让我们回顾一下并分解 `emitted()` 的输出。每个键包含测试期间发出的不同值
79+
让我们回顾一下并分解 `emitted()` 的输出。每个键都包含测试期间触发的不同值
8180

8281
```js
8382
// console.log(wrapper.emitted('increment'))
@@ -89,7 +88,7 @@ test('emits an event with count when clicked', () => {
8988

9089
## 断言复杂事件
9190

92-
假设我们的 `<Counter>` 组件现在需要发出一个包含附加信息的对象。例如,我们需要告诉任何监听 `@increment` 事件的父组件 `count` 是偶数还是奇数:
91+
假设我们的 `<Counter>` 组件现在需要触发一个包含附加信息的对象。例如,我们需要告诉任何监听 `@increment` 事件的父组件 `count` 是偶数还是奇数:
9392

9493
```js {12-15}
9594
const Counter = {
@@ -112,7 +111,7 @@ const Counter = {
112111
}
113112
```
114113

115-
和之前一样,我们需要在 `<button>` 元素上触发 `click` 事件。然后,我们使用 `emitted('increment')` 确保发出了正确的值
114+
正如我们之前所做的那样,我们需要在 `<button>` 元素上触发 `click` 事件。然后,我们使用 `emitted('increment')` 来确保触发了正确的值
116115

117116
```js
118117
test('emits an event with count when clicked', () => {
@@ -124,8 +123,7 @@ test('emits an event with count when clicked', () => {
124123
// 我们“点击”了两次,因此 `increment` 的数组应该有两个值。
125124
expect(wrapper.emitted('increment')).toHaveLength(2)
126125

127-
// 然后,我们可以确保 `wrapper.emitted('increment')` 的每个元素
128-
// 包含一个带有预期对象的数组。
126+
// 然后,我们可以确保 `wrapper.emitted('increment')` 的每个元素包含一个带有预期对象的数组。
129127
expect(wrapper.emitted('increment')[0]).toEqual([
130128
{
131129
count: 1,
@@ -150,6 +148,6 @@ test('emits an event with count when clicked', () => {
150148

151149
## 结论
152150

153-
- 使用 `emitted()` 访问从 Vue 组件发出的事件
154-
- `emitted(eventName)` 返回一个数组,其中每个元素代表一个已发出的事件
155-
- 参数存储在 `emitted(eventName)[index]` 中,按发出顺序以数组形式呈现
151+
- 使用 `emitted()` 来访问从 Vue 组件触发的事件
152+
- `emitted(eventName)` 返回一个数组,其中每个元素代表一个已触发的事件
153+
- 参数存储在 `emitted(eventName)[index]` 中,按触发顺序以数组形式呈现

0 commit comments

Comments
 (0)