简体   繁体   English

ng build --prod在Docker容器上不起作用

[英]ng build --prod not working on Docker container

I am trying to deploy a MEAN stack application on a Docker container. 我正在尝试在Docker容器上部署MEAN堆栈应用程序。 The code for the MEAN stack application is at https://github.com/shivkiyer/quicknoteapp MEAN堆栈应用程序的代码位于https://github.com/shivkiyer/quicknoteapp

I created a Docker file for the image: 我为映像创建了一个Docker文件:

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y git && \
    apt-get install -y curl

RUN apt-get install -y build-essential checkinstall libssl-dev

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash

RUN apt-get install -y nodejs

RUN apt-get update

RUN npm -v

RUN npm cache clean -f && \
    npm install -g @angular/cli

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 && \
    echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list && \
    apt-get update

RUN apt-get install -y --no-install-recommends apt-utils && \
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata

RUN apt-get install debconf-utils

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mongodb-org

EXPOSE 8080 4200

The Docker image is built OK. Docker镜像构建成功。 I create a container with: docker container run -it --name meanstack2 --mount type=bind,source="$(pwd)",target=/home/mean --publish 4200:4200 meanstack 我用以下方法创建一个容器:docker container run -it --name meanstack2 --mount type = bind,source =“ $(pwd)”,target = / home / mean --publish 4200:4200 meanstack

Since I gave my image a tag of meanstack. 因为我给图像加了一个meanstack标签。 In the interactive mode, I create the /data/db directory needed by MongoDB. 在交互模式下,我创建MongoDB所需的/ data / db目录。 I start Mongo with the command "mogod &". 我以“ mogod&”命令启动Mongo。 I run npm install in both the Node JS and Angular directories. 我在Node JS和Angular目录中都运行npm install。

Now the problem starts. 现在问题开始了。 When I try to do "ng build --prod" in the Angular CLI directory blog-frontend, I get all kinds of weird errors. 当我尝试在Angular CLI目录blog-frontend中执行“ ng build --prod”时,出现各种奇怪的错误。 When I do "ng build", it works perfectly OK and I am able to view the app on my browser after doing npm start in the Node Js directory "blog-backend". 当我执行“ ng build”时,它可以正常工作,并且可以在Node Js目录“ blog-backend”中执行npm start之后在浏览器上查看该应用程序。

Why doesn't --prod work? 为什么--prod不起作用? These are the errors I get: 这些是我得到的错误:

ERROR in ng:///home/mean/blog-frontend/src/app/login-register/login-register.component.html (19,36): Property 'onLogin' does not exist on type 'LoginRegisterComponent'.
ERROR in ng:///home/mean/blog-frontend/src/app/login-register/login-register.component.html (85,42): Property 'resisterUser' does not exist on type 'LoginRegisterComponent'.
ERROR in ng:///home/mean/blog-frontend/src/app/welcome-page/welcome-page.component.html (35,38): Property 'onLogin' does not exist on type 'WelcomePageComponent'.
ERROR in ng:///home/mean/blog-frontend/src/app/note/note-form/note-form.component.html (89,15): Supplied parameters do not match any signature of call target.
ERROR in ng:///home/mean/blog-frontend/src/app/note/note-form/note-form.component.html (95,15): Supplied parameters do not match any signature of call target.
ERROR in /home/mean/blog-frontend/src/app/shared/topics.service.ts (10,33): Property 'configSettings' does not exist on type '{ production: boolean; baseURL: string; }'.
ERROR in /home/mean/blog-frontend/src/app/shared/sub-topics.service.ts (12,33): Property 'configSettings' does not exist on type '{ production: boolean; baseURL: string; }'.
ERROR in /home/mean/blog-frontend/src/app/shared/note.service.ts (14,33): Property 'configSettings' does not exist on type '{ production: boolean; baseURL: string; }'.

The flag --prod includes Angular AOT compilation and it has some restrictions of how we need to write code. 标志--prod包括Angular AOT编译,它对我们编写代码的方式有一些限制。

1) login-register.component.html (19,36): Property 'onLogin' does not exist on type 'LoginRegisterComponent'. 1) login-register.component.html(19,36):类型'LoginRegisterComponent'上不存在属性'onLogin'。

It means that the template defines onLogin method but you didn't define it on LoginRegisterComponent class. 这意味着模板定义了onLogin方法,但是您没有在LoginRegisterComponent类上定义它。

export class LoginRegisterComponent implements OnInit {
  onLogin() { <== must be here
  }

2) login-register.component.html (85,42): Property 'resisterUser' does not exist on type 'LoginRegisterComponent' 2) login-register.component.html(85,42):类型“ LoginRegisterComponent”上不存在属性“ resisterUser”

the same as above: 与上述相同:

export class LoginRegisterComponent implements OnInit {
  onLogin() {
  }

  resisterUser() { <== add this
  }

3) note-form.component.html (89,15): Supplied parameters do not match any signature of call target. 3) note-form.component.html(89,15):提供的参数与调用目标的任何签名都不匹配。

In note-form.component.html you call addText method without parameters: note-form.component.html addText ,调用不带参数的addText方法:

(click)="addText()"

while in component you defined this method with required parameter textValue 而在组件中,您使用必需的参数textValue 定义了此方法

addText(textValue: any) {
  ...
}

To fix it just make it optional 要修复它,只需使其可选

addText(textValue?: any) {
  ...
}

4) note-form.component.html (95,15): Supplied parameters do not match any signature of call target. 4) note-form.component.html(95,15):提供的参数与调用目标的任何签名都不匹配。

The same with addCode addCode相同

template: 模板:

(click)="addCode()"

component: 零件:

addCode(codeValue: any) {
  ...
}

Fix: 固定:

addCode(codeValue?: any) {
  ...
}

5) Property 'configSettings' does not exist on type. 5) 属性“ configSettings”在类型上不存在。

This error has nothing to do with AOT but rather with Angular CLI restriction. 此错误与AOT无关,而与Angular CLI限制无关。

Shape of your production config should match the shape of development config. 生产配置的形状应与开发配置的形状匹配。

environment.ts environment.ts

export const environment = {
  production: false,
  configSettings: {
    baseURL: 'http://localhost:4200/api'
  }
};

with the config above you have to define production config with the same properties but you have: 使用上面的配置,您必须使用相同的属性定义生产配置,但是您必须:

environment.prod.ts environment.prod.ts

export const environment = {
  production: true,
  baseURL: '/api'
};

Solution: 解:

environment.prod.ts environment.prod.ts

export const environment = {
  production: true,
  configSettings: {
    baseURL: '/api'
  }
};

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

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