Angular 6私有方法 [英] Angular 6 private methods

查看:194
本文介绍了Angular 6私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在从5升级到Angular6.我们有一个共享库,我们遇到了构建错误.作为一家Java商店,我们习惯于将组件方法和属性标记为私有.在Angular 6中,构建我们的库时(转换并使用了新的库CLI功能之后),我们得到:

We are upgrading to Angular 6 from 5. We have a shared library that we are getting build errors. Being a Java shop, we got in the habit of marking our component methods and attributes private. In Angular 6 when building our library (after converting and using the new library CLI capability), we get:

Property 'getCurrentYear' is private and only accessible within class.

实际上,模板html中使用的任何属性或方法都不能再在组件类上标记为私有.当然,我们可以通过删除专用"修饰符来解决此问题.当我们使用 https://github.com制作我们的库时,在角度5中不是这种情况./raphael-volt/ng2-testable-lib .

In effect any attribute or method used in a template html cannot be marked private anymore on the component class. Of course we can fix this by removing the 'private' modifier. This was not the case in angular 5 when we produced our library using https://github.com/raphael-volt/ng2-testable-lib.

奇怪的是,这仅在编译我们的库时发生.我们将一个应用程序升级到了angular 6,该应用程序还具有针对组件的专有属性和方法/模板中的用法,在那里没有问题.

Oddly enough, this ONLY happens when compiling our library. We upgraded an app to angular 6 that also has private attributes and methods on the component / usage in template and no issues there.

我们找到错误了吗?有我们不遵守的最佳实践吗?

Did we find a bug? Is there a best practice we are not adhering to?

推荐答案

在Angular中,我们有2种编译模型

In Angular we have 2 models of compilation

  • JIT-即时编译:顾名思义,JIT编译可在浏览器中的即时"编译应用程序. 运行时.

  • JIT - Just-in-Time Compilation : JIT compilation as the name implies, compiles the application Just-in-Time in the browser at runtime.

AoT-提前编译: AoT编译可在构建时编译应用程序.

AoT - Ahead-of-Time Compilation : AoT compilation compiles the application at build time.

默认情况下,使用开发版本(即ng serve),我们将获得JIT编译.这就是它的工作方式.应用程序代码和角度编译器一起由浏览器下载.在运行时,当向应用程序发出请求时,浏览器中的JIT编译器会在应用程序代码执行之前对其进行编译.

By default, with the development build i.e ng serve we get JIT compilation. This is how it works. The application code along with the angular compiler is downloaded by the browser. At runtime, when a request is issued to the application, the JIT-compiler in the browser compiles the application code before it is executed.

使用生产版本,即ng build --prod,我们将对AoT进行编译,从而对角度应用程序进行了预编译.因此,这意味着浏览器将加载可执行代码,以便它可以立即呈现应用程序,而无需等待首先编译应用程序.

with the production build i.e ng build --prod we get AoT compilation the angular application is pre-compiled. So this means the browser loads executable code so it can render the application immediately, without waiting to compile the application first.

TypeScript public没关系,但private没关系

TypeScript public doesn't matter but private does

来自Angular文档
所有 data bound 属性必须是TypeScript公共的 特性. Angular永远不会绑定到TypeScript私有属性.

From Angular Docs
All data bound properties must be TypeScript public properties. Angular never binds to a TypeScript private property.

实际上,它确实绑定到private属性,但未绑定到AoT mode

Actually, it does bind to the private properties, but not in AoT mode

Why AOT Compiler requires public properties, while non-AOT allows private properties?

使用JIT,我们将所有代码转换为ES5,然后在运行时执行绑定.在此过程中,所有可见性修饰符都丢失了,因此您说publicprivate都无关紧要.

With JIT we convert all the code to ES5 and then at runtime, we do the bindings. All the visibility modifiers are lost in that process, so it doesn't matter if you say public or private for that.

另一方面,使用AoT,我们为模板生成了一些打字稿代码,这些代码将尝试访问这些字段.如果它们是private,则它们根本无法访问这些属性,因此,您需要将它们设置为public.

On the other hand, with AoT, we generate some typescript code for our templates, that will try to access those fields. If they are private, they simply cannot access those properties, hence, you need to put them as public.

这篇关于Angular 6私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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