From 101a16560bb8518410d289218f89a6e8e5717251 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Sat, 28 May 2016 14:31:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E6=8C=87=E5=8D=97-=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=9E=8B=E6=8C=87=E4=BB=A4=20=E5=88=9D=E8=AF=91?= =?UTF-8?q?=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../latest/guide/structural-directives.jade | 182 ++++++++++++++++-- 1 file changed, 169 insertions(+), 13 deletions(-) diff --git a/public/docs/ts/latest/guide/structural-directives.jade b/public/docs/ts/latest/guide/structural-directives.jade index 2440e0cf22..53e088946c 100644 --- a/public/docs/ts/latest/guide/structural-directives.jade +++ b/public/docs/ts/latest/guide/structural-directives.jade @@ -139,39 +139,64 @@ figure.image-display detaches it from DOM events (the attachments that it made) and destroys the component. The component can be garbage-collected (we hope) and free up memory. - + 而`ngIf`不同。 + 把`ngIf`设置为假**将会**影响到组件的资源消耗。 + Angular会从DOM中移除该元素,停止相关组件的变更检测,把它从DOM事件中摘掉(事件是组件造成的附加项),并销毁组件。 + 组件会被垃圾回收(希望如此)并释放内存。 Components often have child components which themselves have children. All of them are destroyed when `ngIf` destroys the common ancestor. This cleanup effort is usually a good thing. + 组件通常还有子组件,子组件还有自己的子组件。 + 当`ngIf`销毁这个祖先组件时,它们全都会被销毁。 + 这种清理工作通常会是好事。 + Of course it isn't *always* a good thing. It might be a bad thing if we need that particular component again soon. + 当然,它也并不*总是*好事。 + 如果我们很快就会再次需要这个组件,它就变成坏事了。 + The component's state might be expensive to re-construct. When `ngIf` becomes `true` again, Angular recreates the component and its subtree. Angular runs every component's initialization logic again. That could be expensive ... as when a component re-fetches data that had been in memory just moments ago. + + 重建组件的状态可能是昂贵的。 + 当`ngIf`重新变为`true`的时候,Angular会重新创建该组件及其子树。 + Angular会重新运行每个组件的初始化逻辑。那可能会很昂贵……比如当组件需要重新获取刚刚还在内存中的数据时。 .l-sub-section :marked *Design thought*: minimize initialization effort and consider caching state in a companion service. + + *设计思路*:要最小化初始化的成本,并考虑把状态缓存在一个伴生的服务中。 :marked Although there are pros and cons to each approach, in general it is best to use `ngIf` to remove unwanted components rather than hide them. + 虽然每种方法都有各自的优点和缺点,但使用`ngIf`来移除不需要的组件通常都会比隐藏它们更好一些。 + **These same considerations apply to every structural directive, whether built-in or custom.** We should ask ourselves — and the users of our directives — to think carefully about the consequences of adding and removing elements and of creating and destroying components. + **同样的考量也适用于每一个结构型指令,无论是内建的还是自定义的。** + 我们应该让自己以及指令的使用者仔细考虑下添加元素、移除元素以及创建和销毁组件的后果。 + Let's see these dynamics at work. For fun, we'll stack the deck *against* our recommendation and consider a component called `heavy-loader` that ***pretends*** to load a ton of data when initialized. + 让我们在实践中看看这些变化。为了好玩儿,我们设想在甲板上有个叫`heavy-loader`(重型起重机)的组件,它会***假装***在初始化时装载一吨数据。 + We'll display two instances of the component. We toggle the visibility of the first one with CSS. We toggle the second into and out of the DOM with `ngIf`. + 我们将显示该组件的两个实例。我们使用CSS切换第一个实例的可见性,用`ngIf`把第二个实例添加到DOM和将其移除。 + +makeTabs( `structural-directives/ts/app/structural-directives.component.html, structural-directives/ts/app/heavy-loader.component.ts`, @@ -183,6 +208,9 @@ figure.image-display using the built-in `ngOnInit` and `ngOnDestroy` [lifecycle hooks](lifecycle-hooks.html). Here it is in action: + 借助内建的`ngOnInit`和`ngOnDestroy`[生命周期钩子](lifecycle-hooks.html),我们还能记录到组件的创建或销毁过程。 + 下面是它的操作效果: + figure.image-display img(src='/resources/images/devguide/structural-directives/heavy-loader-toggle.gif' alt="heavy loader toggle") @@ -191,159 +219,269 @@ figure.image-display First we toggle the component's visibility repeatedly. The component never leaves the DOM. When visible it's always the same instance and the log is quiet. + 开始的时候,两个组件都在DOM中。 + 首先我们重复切换组件的可见性。组件从未离开过DOM节点。 + 当可见时,它总是同一个实例,而日志里什么都没有。 + Then we toggle the second component with `ngIf`. We create a new instance every time and the log shows that we're paying a heavy price to create and destroy it. + 当我们切换使用`ngIf`的第二个实例时。 + 我们每次都会创建新的实例,而日志中显示,我们为了创建和销毁它付出了沉重的代价。 + If we really expected to "wink" the component like this, toggling visibility would be the better choice. In most UIs, when we "close" a component we're unlikely see it again for a long time, if ever. The `ngIf` would be preferred in that case. + 如果我们真的期望像这样让组件“眨眼”,切换可见性就会是更好的选择。 + 在大多数UI中,当我们“关闭”一个组件时,在相当长时间内都不大可能想再见到它 —— 假如不是再也不见的话。 + 在这种情况下,我们会更喜欢`ngIf`。 + .l-main-section :marked ## The *<template>* tag + ## *<template>*标签 Structural directives, like `ngIf`, do their magic by using the [HTML 5 template tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template). - Outside of an Angular app, the `