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

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

问题描述

Angular 2 rc 6typescript 2node 4.5.0npm 2.15.9 on <代码>Windows 7

我正在尝试从 Just-in-Time 转向 Ahead-of-Time 编译,我依赖这些资源:

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

我知道我需要运行编译器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. 我从生产文件夹中清除了所有应用文件,但保留了 3rd 方库(例如:Angular 2、Angular 2 Material)

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.css.shim.ts 文件.另外,.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. 如果我此时尝试运行该应用程序,我会看到所有 .html 文件的 404 not found 错误持有组件模板

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) 移动到生产目录并运行应用程序.它运行良好,但仍使用即时编译(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 更改为:

6. I change my main.ts to:

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 类.生成的类和组件现在是 2 个独立的类,您不能跨类访问私有成员.

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天全站免登陆