Skip to content

translated components-props.md #702

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 2 commits into from
Apr 26, 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
314 changes: 314 additions & 0 deletions src/v2/guide/components-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
---
title: Prop
type: guide
order: 102
---

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

## Prop 的大小写 (camelCase vs kebab-case)

HTML 中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符。这意味着当你使用 DOM 中的模板时,camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab-case (短横线分隔命名) 命名:

``` js
Vue.component('blog-post', {
// 在 JavaScript 中是 camelCase 的
props: ['postTitle'],
template: '<h3>{{ postTitle }}</h3>'
})
```

``` html
<!-- 在 HTML 中是 kebab-case 的 -->
<blog-post post-title="hello!"></blog-post>
```

重申一次,如果你使用字符串模板,那么这个限制就不存在了。

## 静态的和动态的 Prop

现在,你已经知道了可以想这样给 prop 传入一个静态的值:
Copy link
Member

Choose a reason for hiding this comment

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

像这样


```html
<blog-post title="My journey with Vue"></blog-post>
```

你也知道 prop 可以通过 `v-bind` 动态赋值,例如:

```html
<blog-post v-bind:title="post.title"></blog-post>
```

在上述两个示例中,我们传入的值都是字符串类型的,但实际上_任何_类型的值都可以传给一个 prop。

### 传入一个数字

```html
<!-- 即便 `42` 是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<blog-post v-bind:likes="42"></blog-post>

<!-- 用一个变量进行动态赋值。-->
<blog-post v-bind:likes="post.likes"></blog-post>
```

### 传入一个布尔值

```html
<!-- 包含该 prop 没有任何值的时候都意味着 `true`。-->
Copy link
Member

Choose a reason for hiding this comment

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

包括 prop 没有值的情况在内,都意味着 true

<blog-post favorited></blog-post>

<!-- 即便 `false` 是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<base-input v-bind:favorited="false">

<!-- 用一个变量进行动态赋值。-->
<base-input v-bind:favorited="post.currentUserFavorited">
```

### 传入一个数组

```html
<!-- 即便数组是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<blog-post v-bind:comment-ids="[234, 266, 273]"></blog-post>

<!-- 用一个变量进行动态赋值。-->
<blog-post v-bind:comment-ids="post.commentIds"></blog-post>
```

### 传入一个对象

```html
<!-- 即便对象是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<blog-post v-bind:comments="{ id: 1, title: 'My Journey with Vue' }"></blog-post>

<!-- 用一个变量进行动态赋值。-->
<blog-post v-bind:post="post"></blog-post>
```

### 传入一个对象的所有属性

如果你想要讲一个对象的所有属性都作为 prop 传入,你可以使用不带参数的 `v-bind` (取代 `v-bind:prop-name`)。例如,对于一个给定的对象 `post`:
Copy link
Member

Choose a reason for hiding this comment

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

讲 → 将


``` js
post: {
id: 1,
title: 'My Journey with Vue'
}
```

下面的模板:

``` html
<blog-post v-bind="post"></blog-post>
```

等价于:

``` html
<blog-post
v-bind:id="post.id"
v-bind:title="post.title"
></blog-post>
```

## 单向数据流

所有的 prop 都使得其父子 prop 之间形成了一个**单向下行绑定**:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。这样会防止从子组件意外改变父级组件的状态,从而导致你的应用的数据流向难以理解。
Copy link
Member

Choose a reason for hiding this comment

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

后面两个 prop 原文是 property,我们翻译么?

Copy link
Member Author

Choose a reason for hiding this comment

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

我理解是等价的吧,所以直接换成了 prop


额外的,每次父级组件发生更新时,子组件中所有的 prop 都将会刷新为最新的值。这意味着你**不**应该在一个子组件内部改变 prop。如果你这样做了,Vue 会在命令行中发出警告。
Copy link
Member

Choose a reason for hiding this comment

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

console 要不要统一翻译成(浏览器)控制台?


这里有两种常见的试图改变一个 prop 的情形:

1. **这个 prop 用来传递一个初始值;这个子组件接下来希望将其作为一个本地的 prop 数据来使用。**在这种情况下,最好定义一个本地的 data 属性并将这个 prop 用作其初始值:

``` js
props: ['initialCounter'],
data: function () {
return {
counter: this.initialCounter
}
}
```

2. **这个 prop 以一种原始的值传入且需要做二次加工。**在这种情况下,最好使用这个 prop 的值来定义一个计算属性:
Copy link
Member

Choose a reason for hiding this comment

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

需要进行转换?


``` js
props: ['size'],
computed: {
normalizedSize: function () {
return this.size.trim().toLowerCase()
}
}
```

<p class="tip">注意在 JavaScript 中对象和数组是通过引用传入的,所以对于一个数组或对象类型的 prop 来说,在子组件中改变这个对象或数组本身**将会**影响到父组件的状态。</p>

## Prop 验证

我们可以为组件的 prop 定制需求。如果有一个需求没有被满足,则 Vue 会在浏览器控制台中警告你。这尤其在开发一个会被别人用到的组件的时候非常有帮助。
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.

specify → 指定


To specify prop validations, you can provide an object with validation requirements to the value of `props`, instead of an array of strings. For example:
Copy link
Member

Choose a reason for hiding this comment

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

为了定制 prop 的验证方式,你可以为 `props` 中的值提供一个带有验证需求的对象,而不是一个字符串数组。例如:
Copy link
Member

Choose a reason for hiding this comment

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

specify → 指定


``` js
Vue.component('my-component', {
props: {
// 基础的类型检查 (`null` 匹配任何类型)
propA: Number,
// 多个可能的类型
propB: [String, Number],
// 必填的字符串
propC: {
type: String,
required: true
},
// 带有默认值的数字
propD: {
type: Number,
default: 100
},
// 带有默认值的对象
propE: {
type: Object,
// 对象或数组且一定会从一个工厂函数返回默认值
default: function () {
return { message: 'hello' }
}
},
// 自定义验证函数
propF: {
validator: function (value) {
// 这个值必须匹配下列字符串中的一个
return ['success', 'warning', 'danger'].indexOf(value) !== -1
}
}
}
})
```

当 prop 验证失败的时候,(开发环境构建版本的) Vue 将会产生一个命令后的警告。
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">注意那些 prop 会在一个组件实例被创建**之前**进行验证,所以实例的属性 (如 `data`、`computed` 等) 在 `default` 或 `validator` 函数中是不可用的。</p>
Copy link
Member

Choose a reason for hiding this comment

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

被创建的被是不是可以去掉


### 类型检查

`type` 可以是下列原生构造函数中的一个:

- `String`
- `Number`
- `Boolean`
- `Function`
- `Object`
- `Array`
- `Symbol`

额外的,`type` 还可以是一个自定义的构造函数,并且其验证方式内部是通过 `instanceof` 来断言的。例如,给定下列现成的构造函数:
Copy link
Member

Choose a reason for hiding this comment

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

并且通过 instanceof 来进行检查确认?


```js
function Person (firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
```

你可以使用:

```js
Vue.component('blog-post', {
props: {
author: Person
}
})
```

来验证 `author` prop 的值是否是通过 `new Person` 创建的。

## 非 Prop 的特性

一个非 prop 特性是指传向一个组件,但是该组件并没有相应 prop 定义的特性。

因为显性定义的 prop 适用于向一个子组件传入信息,组件库的作者无法永远预见到组件使用时的上下文。这也是为什么组件可以接受任意的特性,而这些特性会被添加到这个组件的根元素上。
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.

显性定义的 prop 适用于向一个子组件传入信息,然而组件库的作者并不总能预见组件使用时的上下文(组件会被用于怎样的场景)?


例如,想象一下你通过一个 Bootstrap 插件使用了一个第三方的 `<bootstrap-data-input>` 组件,这个插件需要在其 `<input>` 上用到一个 `data-date-picker` 特性。我们可以将这个特性添加到你的组件实例上:

``` html
<bootstrap-date-input data-date-picker="activated"></bootstrap-date-input>
```

然后这个 `data-date-picker="activated"` 特性就会自动添加到 `<bootstrap-date-input>` 的根元素上。

### 替换/合并已有的特性

想象一下 `<bootstrap-date-input>` 的模板是这样的:

``` html
<input type="date" class="form-control">
```

为了给我们的日期选择器插件定制一个主题,我们可能需要像这样添加一个特别的 class:
Copy link
Member

Choose a reason for hiding this comment

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

类名/`class`


``` html
<bootstrap-date-input
data-date-picker="activated"
class="date-picker-theme-dark"
></bootstrap-date-input>
```

在这种情况下,我们定义了两个不同的 `class` 的值:

- `form-control`,这是在组件的模板内设置好的
- `date-picker-theme-dark`,这是从组件的父级传入的

对于绝大多数特性来说,从外部提供给组件的值会替换掉组件内部设置好的值。所以如果传入 `type="text"` 就会替换掉 `type="date"` 并把它破坏!庆幸的是,`class` 和 `style` 特性会稍微智能一些,即两边的值会被合并起来,从而得到最终的值:`form-control date-picker-theme-dark`。

### 禁用特性继承

如果你**不**希望组件的根元素继承特性,你可以设置在组件的选项中设置 `inheritAttrs: false`。例如:

```js
Vue.component('my-component', {
inheritAttrs: false,
// ...
})
```

尤其是它可以和实例的 `$attrs` 属性结合使用,该属性包含了传递给一个组件的特性名和特性值,例如:
Copy link
Member

Choose a reason for hiding this comment

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

这尤其适合配合实例的 $attrs 属性使用?


```js
{
class: 'username-input',
placeholder: 'Enter your username'
}
```

有了 `inheritAttrs: false` 和 `$attrs`,你就可以手动决定这些特性会被赋予哪个元素。在撰写[基础组件](../style-guide/#基础组件名-强烈推荐)的时候是常会用到的:

```js
Vue.component('base-input', {
inheritAttrs: false,
props: ['label', 'value'],
template: `
<label>
{{ label }}
<input
v-bind="$attrs"
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
</label>
`
})
```

这个模式运行你在使用基础组件的时候更像是使用原始的 HTML 元素,而不会担心哪个元素是真正的根元素:
Copy link
Member

Choose a reason for hiding this comment

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

运行 → 允许


```html
<base-input
v-model="username"
class="username-input"
placeholder="Enter your username"
></base-input>
```