You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/components.md
+19-39Lines changed: 19 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -351,19 +351,15 @@ new Vue({
351
351
<compv-bind:some-prop="1"></comp>
352
352
```
353
353
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
+
### 单向数据流
357
355
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.
<pclass="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>
Or if you're using Babel with ES2015, the much simpler:
428
+
或者用Babel和ES2015可以更简单:
433
429
434
430
```js
435
431
data () {
@@ -439,38 +435,35 @@ data () {
439
435
}
440
436
```
441
437
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验证
445
439
446
-
Instead of defining the props as an array of strings, you can use an object with validation requirements:
// object/array defaults should be returned from a
466
-
// factory function
459
+
// 对象/数组的默认值应当由一个函数返回
467
460
propE: {
468
461
type:Object,
469
462
default:function () {
470
463
return { message:'hello' }
471
464
}
472
465
},
473
-
//custom validator function
466
+
//自定义验证函数
474
467
propF: {
475
468
validator:function (value) {
476
469
return value >10
@@ -480,8 +473,6 @@ Vue.component('example', {
480
473
})
481
474
```
482
475
483
-
The `type` can be one of the following native constructors:
484
-
485
476
`type` 可以是下面原生构造器:
486
477
487
478
- String
@@ -491,47 +482,36 @@ The `type` can be one of the following native constructors:
491
482
- Object
492
483
- Array
493
484
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
-
498
485
`type` 也可以是一个自定义构造器,使用 `instanceof` 检测。
499
486
500
487
当 prop 验证失败了,Vue 将拒绝在子组件上设置此值,如果使用的是开发版本会抛出一条警告。
501
488
502
-
## Parent-Child Communication
489
+
## 父子组件通信
503
490
504
-
### Parent Chain
491
+
### 父链
505
492
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.
<pclass="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>
509
496
510
497
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.
511
498
512
499
For managing state in more complex applications, [vuex](https://github.com/vuejs/vuex/) is recommended as an officially supported companion library.
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:
530
511
531
512
532
513
### 自定义事件
533
514
534
-
Vue 实例实现了一个自定义事件接口,用于在组件树中通信。这个事件系统独立于原生 DOM 事件,用法也不同。
0 commit comments