Angular 2 Ahead-of-Time编译器:我必须让所有的类属性公开吗? [英] Angular 2 Ahead-of-Time compiler: must I make all class properties public?

查看:166
本文介绍了Angular 2 Ahead-of-Time编译器:我必须让所有的类属性公开吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角度2 rc 6 typescript 2 节点4.5.0 npm 2.15.9 Windows 7

我正在尝试从即时到时间的编译,我依靠这些资源:

I'm trying to move from Just-in-Time to Ahead-of-Time compilation and I'm relying on these resources:

  • Angular 2 - Ahead-of-time compilation how to
  • https://github.com/angular/angular/tree/master/modules/@angular/compiler-cli#angular-template-compiler

我明白我需要运行编译器 ngc 来生成 ngfactory.ts 文件,我需要更改 main.ts 以使用 platformBrowser 而不是 platformBrowserDynamic 引导。我已经打了一个路障,不知道如何继续。

I understand that I need to run the compiler ngc to generate ngfactory.ts files, and that I need to change main.ts to use platformBrowser instead of platformBrowserDynamic to bootstrap. I've hit a roadblock though and don't know how to proceed.

1。我已经确认应用程序运行没有错误使用即时编译。我的 main.ts 是:

1. I've confirmed that the App runs without errors using Just-in-Time compilation. My main.ts is:

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

2。我清除我的生产文件夹中的所有应用程序文件,但保存第三方库(例如:角度2,角度2材质)

2. I clear all my app files from my production folder, but keep 3rd party libraries (eg: Angular 2, Angular 2 Material)

3。我运行node_modules /。 bin / ngc-p ./这个运行没有输出到控制台。我看到每个 .ts 组件和模块的 .ngfactory.ts 文件。我也看到一个 .css.shim.ts 文件,我的每个 .css 保存组件样式。另外, .js .js.map 文件已经被放置在生产目录

3. I run "node_modules/.bin/ngc" -p ./ This runs with no output to the console. I see an .ngfactory.ts file for each of my .ts components and modules. I also see a .css.shim.ts file for each of my .css that held component styles. In addition, .js and .js.map files have been transpiled and placed in the production directory

4。如果我尝试运行该应用程序,我看到 404未找到错误对于持有组件模板的所有 .html 文件

4. If I try to run the app at this point, I see 404 not found errors for all the .html files that held component templates

5。我手动移动所有模板文件( .html )到生产目录并运行App。它运行正常,但它仍然使用即时编译(255个请求,包括 compiler.umd.js

5. I manually move all template files (.html) to production dir and run the App. It runs fine, but it still uses Just-in-Time compilation (255 requests, including compiler.umd.js)

6。我将 main.ts 更改为:

enableProdMode();
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

自己也没有区别,因为新的代码还没有被编译。但是,我不知道该怎么做。

On its own, this makes no difference since the new code has not been compiled. However, I don't know what to do from here.

我应该再次运行 ngc 如果是这样,我会收到以下类型的错误:

Should I run ngc again? If so I get lots of errors of the type:

Error at C:/path/to/notify.component.ngfactory.ts:113:41: Property 'visible' is private and only accessible within class 'NotifyComponent'
... (many more like that with lots of properties from lots of components)
Compilation failed

使用AoT编译是否意味着我必须将我的所有类属性都公开?我是否缺少一个步骤?

Does using AoT compilation mean that I must make all my class properties public? Am I missing a step?

推荐答案

对于给定的组件,其所有成员(方法,属性)由其模板访问必须在提前编译场景中公开。这是由于模板变成TS类的事实。一个生成的类和一个组件现在是两个不同的类,你不能访问私人成员跨类。

For a given component all its members (methods, properties) accessed by its template must be public in the ahead-of-time compilation scenario. This is due to the fact that a template is turned into a TS class. A generated class and a component are 2 separate classes now and you can't access private members cross-class.

简而言之:你不能访问你的私人成员如果您想使用预先编译的模板。

In short: you can't access private members in your templates if you want to use ahead-of-time compilation.

这篇关于Angular 2 Ahead-of-Time编译器:我必须让所有的类属性公开吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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