简体   繁体   English

Angular 4 ng serve --prod vs ng serve

[英]Angular 4 ng serve --prod vs ng serve

Shortly, I have an app which is 4.6MB on ng serve.很快,我在 ng serve 上有一个 4.6MB 的应用程序。

When I do:当我这样做时:

ng serve --prod ng 服务 --prod

I get 1MB file size.我得到 1MB 的文件大小。

However, the --prod somehow make my entire application break.但是,--prod 以某种方式使我的整个应用程序中断。

My whole services (promises based) which send a request to the server are no longer working.我向服务器发送请求的整个服务(基于承诺)不再工作。

The bizzar thing is that simply ng serve works perfectly fine AND certain parts of ng serve --prod works fine as well, as long as there is no server request.该bizzar的是,简单地NG服务工作完全正常NG的某些部分服务--prod作品也很有效,只要没有服务器的请求。

I do not post any code since ng serve version works ok.我没有发布任何代码,因为ng serve版本可以正常工作。

The main question is:主要问题是:

Why do I get hat kind of behavior?为什么我会出现类似帽子的行为?

Moreover, at some point, the app based on ng serve --prod suddenly fully worked out of nowhere and then after I restarted the app, once again, a broken app.此外,在某些时候,基于ng serve --prod的应用程序突然突然无处可去,然后在我重新启动应用程序后,再次出现了一个损坏的应用程序。

EDIT: more EXPLICIT details:编辑:更多明确的细节:

I am using Fiddler to make sure all of the details are correct:我正在使用 Fiddler 来确保所有细节都是正确的:

小提琴手图片

As you can see, details are ok.如您所见,细节没问题。

Now for the code which responsible to simulate that request on the client side:现在是负责在客户端模拟该请求的代码:

    login(uName: string, pw: string, type: number): Promise<any> {
    return new Promise(resolve => {
        if (type === 1) {
            const options = new RequestOptions({ headers: this.headers });
            const body = JSON.stringify({ username: uName, password: pw });
            this.http.post(this.loginUrl, body, options)
                .toPromise()
                .then(response => {
                    resolve(response);
                })
                .catch(this.handleError);
        } else if (type === 2 || type === 3) {
            this.http.post(this.loginUrl, { username: uName, token: pw })
                .toPromise()
                .then(response => {
                    resolve(response);
                })
                .catch(this.handleError);
        }
    });
}

Now notice how everything works perfect when I use ng serve only (Network Tab):现在注意当我只使用ng serve时一切都是如何完美的(网络选项卡):

服务

As you can see, I am already logged in and I do get a response.如您所见,我已经登录并且确实收到了回复。

Now,现在,

The moment I do我做的那一刻

ng serve --prod ng 服务 --prod

Suddenly the same login request with the same details no longer works:突然,具有相同详细信息的相同登录请求不再有效:

ng 服务 --prod

This is super bizzar.这是超级怪异。

All of my methods which are responsible for server requests are all the same.我所有负责服务器请求的方法都是一样的。

"Bad Request" with some error code that comes from the server itself (my own server code like "email not filled" which is also bizzar since I do send the correct parameters) “错误请求”带有一些来自服务器本身的错误代码(我自己的服务器代码,如“电子邮件未填写”,这也很奇怪,因为我确实发送了正确的参数)

when you did ng serve --prod angular cli make a production build with tree shaking and AOT (Ahead Of Time) compilation and generate less code compare to normal build.当您执行ng serve --prod angular cli 时,使用摇树和 AOT(Ahead Of Time)编译进行生产构建,并生成比正常构建更少的代码。 You also run in local what it will be like in prod mode您还可以在本地运行 prod 模式下的样子

Means it tree shake all your components and added which ever actually used in your code, not all.意味着它会摇动您的所有组件并添加在您的代码中实际使用过的组件,而不是全部。 That's why you see the vendor.js is really small when you did ng serve --prod这就是为什么当你执行ng serve --prod时你会看到vendor.js真的很小

disadvantage is it is less debuggable as its compiled and minified code缺点是它的可调试性较低,因为它的编译和缩小代码

you can read in detail in the cli documentation which build is doing what.您可以在构建正在做什么的 cli 文档中详细阅读。

--prod is the option of build, default is debug mode --prod是build的选项,默认是debug模式

Let's see an example why application breaks, see we have code like this:让我们看一个为什么应用程序中断的例子,看看我们有这样的代码:

<div (click)="toshow = !toShow">Toggle</div>

imagine, toshow is not defined on the component or by mistake we did a typo say toShow to toshow .想象一下,toshow未在组件上定义或错误,我们做了一个错字说toShowtoshow

In this case ng build and ng serve will work but ng build --prod and ng serve --prod will give error在这种情况下, ng buildng serve将起作用,但ng build --prodng serve --prod会出错

We also faced similar issue and fixed by following these guidelines.我们也遇到了类似的问题,并按照这些指南进行了修复。 The issue is AOT do not support some features which JIT supports.问题是 AOT 不支持 JIT 支持的某些功能。 Please check this link.请检查此链接。 hopefully it will help you.希望它会帮助你。

https://github.com/rangle/angular-2-aot-sandbox https://github.com/rangle/angular-2-aot-sandbox

Angular DOC About AOT Restrictions关于 AOT 限制的 Angular DOC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM