Skip to content

translated components-custom-events.md #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions src/v2/guide/components-custom-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
title: 自定义事件
type: guide
order: 103
---

> 该页面假设你已经阅读过了[组件基础](components.html)。如果你还对组件不太了解,推荐你先阅读它。

## 事件名

跟组件和 prop 不同,事件名不存在任何自动化的大小写转换。而是抛出的事件名需要完全匹配监听这个事件所用的名称。举个例子,如果抛出一个 camelCase 名字的事件:

```js
this.$emit('myEvent')
```

则监听这个名字的 kebab-case 版本是不会有任何效果的:

```html
<my-component v-on:my-event="doSomething"></my-component>
```

跟组件和 prop 不同,事件名不会被用作一个 JavaScript 变量名或属性名,所以就没有理由使用 camelCase 或 PascalCase 了。并且 `v-on` 事件监听器在 DOM 模板中会被自动转换为全小写 (因为 HTML 是大小写不敏感的),所以 `v-on:myEvent` 将会变成 `v-on:myevent`——导致 `myEvent` 不可能被监听到。

因此,我们推荐你**始终使用 kebab-case 的事件名**。

## 自定义组件的 `v-model`

> 2.2.0+ 新增

一个组件上的 `v-model` 默认会利用名为 `value` 的 prop 和名为 `input` 的事件,但是像单选框、复选框等一些输入类型可能会将 `value` 特性用于[不同的目的](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value)。`model` 选项可以用来避免这样的冲突:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input types 这个 input 应该是输入组件的意思,合起来:单选框、复选框等类型的输入组件?


```js
Vue.component('base-checkbox', {
model: {
prop: 'checked',
event: 'change'
},
props: {
checked: Boolean
},
template: `
<input
type="checkbox"
v-bind:checked="checked"
v-on:change="$emit('change', $event.target.checked)"
>
`
})
```

现在在这个组件上使用 `v-model` 当时候:

```js
<base-checkbox v-model="lovingVue"></base-checkbox>
```

这里的 `lovingVue` 的值将会传入这个名为 `checked` 的 prop。同时当 `<base-checkbox>` 抛出一个 `change` 事件并附带一个新的值的时候,这个 `lovingVue` 的属性将会被更新。

<p class="tip">注意你仍然需要在组件的 <code>props</code> 选项里声明 <code>checked</code> 这个 prop。</p>

## 将原生事件绑定到组件

你可能有很多次想要在一个组件的根元素上直接监听一个原生事件。这时,你可以使用 `v-on` 的 `.native` 修饰符:

```html
<base-input v-on:focus.native="onFocus"></base-input>
```

在有的时候这是很有用的,不过在你尝试监听一个类似 `<input>` 的非常特定的元素时,这并不是个好主意。比如上述 `<base-input>` 组件可能做了如下重构,所以根元素实际上是一个 `<label>` 元素:

```html
<label>
{{ label }}
<input
v-bind="$attrs"
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
</label>
```

这时,父级的 `.native` 监听器将会被默默的破坏。它不会产生任何报错,但是 `onFocus` 处理函数不会如你预期的被调用。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

静默失败?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如你预期地


为了解决这个问题,Vue 提供了一个 `$listeners` 属性,它是一个对象,里面包含了作用在这个组件上的所有监听器。例如:

```js
{
focus: function (event) { /* ... */ }
input: function (value) { /* ... */ },
}
```

有了这个 `$listeners` 属性,你就可以配合 `v-on="$listeners"` 将所有的事件监听器指向这个组件的某个特定的子元素。对于类似 `<input>` 的你希望它也可以配合 `v-model` 工作的组件来说,为这些监听器创建一个类似下述 `inputListeners` 的计算属性通常是非常有用的:

```js
Vue.component('base-input', {
inheritAttrs: false,
props: ['label', 'value'],
computed: {
inputListeners: function () {
var vm = this
// `Object.assign` 将所有的对象合并为一个新对象
return Object.assign({},
// 我们从父级添加所有的监听器
this.$listeners,
// 然后我们添加自定义监听器,
// 或覆写一些监听器的行为
{
// 这里确保组件配合 `v-model` 的工作
input: function (event) {
vm.$emit('input', event.target.value)
}
}
)
}
},
template: `
<label>
{{ label }}
<input
v-bind="$attrs"
v-bind:value="value"
v-on="inputListeners"
>
</label>
`
})
```

现在 `<base-input>` 组件是一个**完全透明的包裹器**了,也就是说它可以完全像一个普通的 `<input>` 元素一样使用了:所有跟它相同的特性和监听器的都可以工作。

## `.sync` 修饰符

> 2.3.0+ 新增

在有些情况下,我们可能需要对一个 prop 进行“双向绑定”。不幸的是,真正的双向绑定会带来维护上的问题,因为子组件可以修改父组件,且在父组件和子组件都没有明显的改动来源。

这也是为什么我们推荐以 `update:my-prop-name` 的模式抛出事件取而代之。举个例子,在一个包含 `title` prop 的假设的组件中,我们可以用以下方法表达对其赋新值的意图:

```js
this.$emit('update:title', newTitle)
```

然后父组件可以监听那个事件并根据需要更新一个本地的数据属性。例如:

```html
<text-document
v-bind:title="doc.title"
v-on:update:title="doc.title = $event"
></text-document>
```

为了方便起见,我们为这种模式提供一个缩写,即 `.sync` 修饰符:

```html
<text-document v-bind:title.sync="doc.title"></text-document>
```

当我们用一个对象同时设置多个 prop 的时候,也可以将这个 `.sync` 修饰符和 `v-bind` 配合使用:

```html
<text-document v-bind.sync="doc"></text-document>
```

这样会把 `doc` 对象中的每一个属性 (如 `title`) 都作为一个独立的 prop 传进去,然后各自添加用作更新的 `v-on` 监听器。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用于更新


<p class="tip">请将 <code>v-bind.sync</code> 用在一个字面量的对象上,例如 <code>v-bind.sync="{ title: doc.title }"</code>,因为在解析一个像这样的复杂表达式的时候,有很多边缘情况需要考虑。</p>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句话虽然翻译出来了,但是没有完全理解,同时觉得和上下文对不上……

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://cn.vuejs.org/v2/guide/components.html#sync-%E4%BF%AE%E9%A5%B0%E7%AC%A6
之前的文档写的是支持字面量对象,新版不支持。

Copy link
Contributor

@dear-lizhihua dear-lizhihua Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vuejs/v2.vuejs.org@5adbc5f
原先是这句:will not work,最新的去掉了。到底是支持还是不支持?

Using v-bind.sync with a literal object, such as in v-bind.sync="{ title: doc.title }", will not work. If you want to include multiple, unrelated data properties in the same v-bind.sync, we recommend creating a computed property that returns an object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最新的原文显然是读不通的,可能就像 @dear-lizhihua 所言少了“will not work”。感觉需要和 Chris 确认一下。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我先按照原文加上 will not work 的版本重新翻译了一下

最新的去掉了

@dear-lizhihua 你的意思是什么东西在哪里被去掉了?有点没理解

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在 vuejs.org 的网站上和源代码的 master 分支里不都有“will not work”吗?
https://vuejs.org/v2/guide/components-custom-events.html
https://github.com/vuejs/vuejs.org/blob/master/src/v2/guide/components-custom-events.md
应该是我之前 fork 出来的某个版本没有吧?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就是这个意思吧

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vuejs/v2.vuejs.org#1572
10小时前刚合并进去的,我们讨论的是19小时前的版本

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got,那我就保持现状了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(当然其余的部分仍然需要 review,所以现在不会立刻 merge)