Skip to content

Commit 125331b

Browse files
committed
继续翻译组件 篇
1 parent 97eaa8f commit 125331b

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

src/guide/components.md

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,15 @@ new Vue({
351351
<comp v-bind:some-prop="1"></comp>
352352
```
353353

354-
### One-Way Data Flow
355-
356-
All props form a **one-way-down** binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent's state, which can make your app's data flow harder to reason about.
354+
### 单向数据流
357355

358-
This means you should never mutate props. If you do, Vue will slap your hand with a warning in the console. If you feel the need to mutate a prop, you can instead use it in a computed property or to define the initial value for a data property.
356+
所有的props在父子组件之间形成一个单一向下的数据绑定:父组件属性更新,会相应传递到子组件中,而不是其他方法。防止子组件更新父组件状态,是因为子组件偶然更新父组件状态会让应用数据流变复杂。
359357

360-
<p class="tip">Note that objects and arrays in JavaScript are passed by reference, so if the prop is an array or object, mutating the object or array itself inside the child **will** affect parent state.</p>
358+
绝不更改props的属性。一旦props属性被修改,Vue会在控制台抛出警告。如果需要修改prop属性,可以通过计算属性或者声明一个初始数据。
361359

362-
Here's an example:
360+
<p class="tip">请注意,在JavaScript中对象和数据是通过引用传递的,所以如果prop是一个数组或对象,在子组件中更改对象或数组***会影响父组件状态***</p>
363361

364-
### 单向数据流
365-
366-
所有的props
362+
这有个示例:
367363

368364
``` html
369365
<div id="parent-object-mutation-demo">
@@ -419,7 +415,7 @@ new Vue({
419415
</script>
420416
{% endraw %}
421417

422-
To fix this, you can clone the object with:
418+
为了防止这个问题,可以像这样复制一个对象:
423419

424420
``` js
425421
data: function () {
@@ -429,7 +425,7 @@ data: function () {
429425
}
430426
```
431427

432-
Or if you're using Babel with ES2015, the much simpler:
428+
或者用Babel和ES2015可以更简单:
433429

434430
``` js
435431
data () {
@@ -439,38 +435,35 @@ data () {
439435
}
440436
```
441437

442-
### Prop Validation
443-
444-
It is possible for a component to specify requirements for the props it is receiving. If a requirement is not meant, Vue will emit warnings. This is especially useful when you are authoring a component that is intended to be used by others.
438+
### Prop验证
445439

446-
Instead of defining the props as an array of strings, you can use an object with validation requirements:
440+
组件可以为 props 指定验证要求。当组件给其他人使用时这很有用,因为这些验证要求构成了组件的 API,确保其他人正确地使用组件。此时 props 的值是一个对象,包含验证要求:
447441

448442
``` js
449443
Vue.component('example', {
450444
props: {
451-
// basic type check (`null` means accept any type)
445+
// 基础类型检测 (`null` 意思是任何类型都可以)
452446
propA: Number,
453-
// multiple possible types
447+
// 多种类型 (1.0.21+)
454448
propB: [String, Number],
455-
// a required string
449+
// 必需且是字符串
456450
propC: {
457451
type: String,
458452
required: true
459453
},
460-
// a number with default value
454+
// 数字,有默认值
461455
propD: {
462456
type: Number,
463457
default: 100
464458
},
465-
// object/array defaults should be returned from a
466-
// factory function
459+
// 对象/数组的默认值应当由一个函数返回
467460
propE: {
468461
type: Object,
469462
default: function () {
470463
return { message: 'hello' }
471464
}
472465
},
473-
// custom validator function
466+
// 自定义验证函数
474467
propF: {
475468
validator: function (value) {
476469
return value > 10
@@ -480,8 +473,6 @@ Vue.component('example', {
480473
})
481474
```
482475

483-
The `type` can be one of the following native constructors:
484-
485476
`type` 可以是下面原生构造器:
486477

487478
- String
@@ -491,47 +482,36 @@ The `type` can be one of the following native constructors:
491482
- Object
492483
- Array
493484

494-
In addition, `type` can also be a custom constructor function and the assertion will be made with an `instanceof` check.
495-
496-
When a prop validation fails, Vue will refuse to set the value on the child component and throw a warning if using the development build.
497-
498485
`type` 也可以是一个自定义构造器,使用 `instanceof` 检测。
499486

500487
当 prop 验证失败了,Vue 将拒绝在子组件上设置此值,如果使用的是开发版本会抛出一条警告。
501488

502-
## Parent-Child Communication
489+
## 父子组件通信
503490

504-
### Parent Chain
491+
### 父链
505492

506-
A child component holds access to its parent component as `this.$parent`. A root Vue instance will be available to all of its descendants as `this.$root`. Each parent component has an array, `this.$children`, which contains all its child components.
493+
子组件可以用 `this.$parent` 访问它的父组件。根实例的后代可以用 `this.$root` 访问它。父组件有一个数组 `this.$children`,包含它所有的子元素。
507494

508495
<p class="tip">These properties are made available as an escape hatch to accomodate extreme edge cases. They are not the correct way to access and mutate components in the vast majority of circumstances and if abused, can make your components much more difficult to reason about.</p>
509496

510497
Instead, prefer passing data down explicitly using props. Where data must be shared and mutated by multiple components, a parent component can be used to manage state in a single location. To mutate parent state, custom events can be emitted that parents may choose to listen to or mutation methods can be passed to child components.
511498

512499
For managing state in more complex applications, [vuex](https://github.com/vuejs/vuex/) is recommended as an officially supported companion library.
513500

514-
## 父子组件通信
515-
516-
### 父链
517-
518-
子组件可以用 `this.$parent` 访问它的父组件。根实例的后代可以用 `this.$root` 访问它。父组件有一个数组 `this.$children`,包含它所有的子元素。
519-
520501
尽管可以访问父链上任意的实例,不过子组件应当避免直接依赖父组件的数据,尽量显式地使用 props 传递数据。另外,在子组件中修改父组件的状态是非常糟糕的做法,因为:
521502

522503
1. 这让父组件与子组件紧密地耦合;
523504

524505
2. 只看父组件,很难理解父组件的状态。因为它可能被任意子组件修改!理想情况下,只有组件自己能修改它的状态。
525506

526507

527-
### Custom Events
508+
### 自定义事件
528509

529510
You can use a pattern similar to the EventEmitter in Node.js: a centralized event hub that allows components to communicate, no matter where they are in the component tree. Because Vue instances implement the event emitter interface, you can actually use an empty Vue instance for this purpose:
530511

531512

532513
### 自定义事件
533514

534-
Vue 实例实现了一个自定义事件接口,用于在组件树中通信。这个事件系统独立于原生 DOM 事件,用法也不同。
535515

536516
``` js
537517
var bus = new Vue()

0 commit comments

Comments
 (0)