在手动更改检测方面,Angular Ivy特别允许我们做什么? [英] What does Angular Ivy specifically allow us to do in regards to manual change detection?

查看:69
本文介绍了在手动更改检测方面,Angular Ivy特别允许我们做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文提到

常春藤为未来打开了一些可能性.现在应该可以在没有zone.js的情况下运行应用程序,并以半手动方式处理更改检测(有点像使用React一样).这些API已经存在,但仍处于实验阶段,没有记录在案,并且可能会在不久的将来发生变化.

Ivy opens a few possibilities for the future though. It should now be possible to run an application without zone.js, and to semi-manually handle change detection (a bit like you would with React). These APIs already exist but are experimental, not documented, and will probably change in the near future.

我认为在Ivy之前无需使用zone.js就可以运行应用程序.常春藤是否允许半手动处理变更检测?这些实验性API在哪里?有文件吗?常春藤还使用zone.js吗?

I think it was already possible to run an application without zone.js prior to Ivy. Does Ivy allow to semi-manually handle change detection? Where are those experimental APIs? Any doc? Does Ivy still use zone.js?

我的目标是通过手动触发更改检测来将更改检测降至最低.这样做的最佳选择是什么.具体来说,使用常春藤时最好的选择是什么.

My goal is to limit change detections to a minimum by triggering them manually. What is the best option to do that. Specifically what is the best option when using Ivy.

推荐答案

这是一个很大的话题,在这里,我会尽力回答.

That's a huge topic to cover here, but I'll try to answer.

这个想法实际上是在渲染组件而不在任何模块中声明它们的.

The idea is actually rendering components without declaring them inside any module.

我们为什么要这样做?这很简单-模块不仅仅是组件.模块具有区域,提供程序,注入器,DI等.我们中许多人的模块代表应用程序.有时我们只想创建一个简单的组件并将其呈现在另一个组件中.

Why would we wanna do something like that? It simple - Modules are much more than just components. Modules have zones, providers, injectors, DI, and much more. Modules for a lot of us represent applications. And sometimes we just want to create a simple component and render it in another component.

它将引起什么问题?模块是为我们设置区域的模块.区域是自动触发更改检测的区域.如果我们要在模块外部渲染组件,则不会具有自动更改检测功能.

What's the problem it will cause? Modules are the one who sets up zones for us. Zones are the ones who trigger change detection automatically. If we will render a component outside a module, we won't have automatic change detection.

因此,有了常春藤,我们有了一些新的APIS可以为我们提供帮助:

So, with Ivy, we have a few new APIS that can help us:

ɵrenderComponent()-可以渲染组件而无需在模块中声明它.

ɵrenderComponent() - That can render a component without declaring it in a module.

ɵdetectChanges(); -要手动触发更改检测,但这只是 @ angular/core 中的一个函数,您不再需要DI即可注入 ChangeDetectorRef

ɵdetectChanges(); - To trigger change detection manually, but, it's just a function from @angular/core and you don't need the DI any more to inject the ChangeDetectorRef

ɵmarkDirty()-标记要在下一个更改检测周期中检查的组件.

ɵmarkDirty() - To mark the component to check in the next change detection cycle.

ɵɵdirectiveInject()-在不使用构造函数的情况下,通过函数注入InjectionToken.

ɵɵdirectiveInject() - Inject an InjectionToken in a matter of function, without using the constructor.

如果您问所有所有新API都以前缀的ɵ符号是什么,则意味着这些功能仍处于试验阶段,您不应该将其用于生产.这就是为什么它们没有记录的原因.

If you asking what is this ɵ sign that prefix all those new APIs, that means those functions are still experimental and you shouldn't use them for production yet. And that's also why they are not documented.

对于您的问题-如果您想最大程度地减少组件中CD的使用,只需使用 renderComponent 函数呈现它们,然后自己处理CD.

For your question - if you want to minimize the use of CD in your components, just render them with renderComponent function, and handle CD by yourself.

如果您想了解更多信息,我写了一篇关于该主题的完整博客文章,其中包括许多代码示例.您可以在这里找到它-"

If you want to read more, I wrote a complete blog post exactly about this topic, including a lot of code examples. You can find it here - "The future of standalone components in post Ivy release days"

我还在NG-DE 2019中进行了讨论-" Bye Bye NgModules "

I also gave a talk about it in NG-DE 2019 - "Bye Bye NgModules"

这篇关于在手动更改检测方面,Angular Ivy特别允许我们做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆