angular - ng build 有效,但不适用于 --prod

标签 angular dependency-injection compiler-errors circular-dependency production

我的 > ng serve> ng build命令不会产生任何错误并且应用程序会运行。但是当我运行 > ng build --prod ,它会引发以下错误。我试图在互联网上找到很多东西并尝试了很多东西,比如

  • 重新排序进口
  • 尝试转发Ref
  • 查找循环依赖
  • 等等。

  • 但是,错误仍然存​​在。

    P.S.: Downvoters, please specify the solution first or any digestible reason, or just skip this question without touching it as you can't evaluate the situation. BUT DON'T CREATE NUISANCE.


    C:\wamp\www\ngapp>ng build --prod
     10% building modules 3/3 modules 0 activeWarning: Can't resolve all parameters for LoginService in C:/wamp/www/ngapp/sr
    c/app/abstract/login.service.ts: ([object Object], ?). This will become an error in Angular v6.x
    Warning: Can't resolve all parameters for UserService in C:/wamp/www/ngapp/src/app/abstract/user.service.ts: ([object Ob
    ject], ?). This will become an error in Angular v6.x
    
    Date: 2018-06-30T06:40:13.988Z
    Hash: 72312a1071e597367666
    Time: 10125ms
    chunk {scripts} scripts.385b291e179030219400.js (scripts) 136 kB  [rendered]
    chunk {0} runtime.a66f828dca56eeb90e02.js (runtime) 1.05 kB [entry] [rendered]
    chunk {1} styles.14764545cc77d1b25789.css (styles) 159 kB [initial] [rendered]
    chunk {2} polyfills.207dcc605630215505f5.js (polyfills) 130 bytes [initial] [rendered]
    chunk {3} main.f58b96bf9bf614ca37d4.js (main) 128 bytes [initial] [rendered]
    
    ERROR in : Can't resolve all parameters for UserService in C:/wamp/www/ngapp/src/app/abstract/user.service.ts: ([object
    Object], ?).
    

    login.service.ts
    import { Injectable, forwardRef, Inject } from '@angular/core';
    //import { Http } from '@angular/http';
    import { HttpClient } from '@angular/common/http';
    import { catchError, map, retry } from 'rxjs/operators';
    import { throwError } from 'rxjs';
    import { NotFoundError } from '../errors/notfound-error';
    import { Unauthorized } from '../errors/unauthorized-error';
    import { AppError } from '../errors/app-error';
    import * as GLOBAL from 'globals';
    
    
    interface Credentials {
      username: string,
      password: string
    }
    
    @Injectable({
      providedIn: 'root'  
    })
    
    export abstract class LoginService {
    
    
      // readonly USER_NOT_FOUND = 404;
      // readonly UNAUTHENTICATED = 401;
    
      private http: HttpClient;
    
      constructor(@Inject(forwardRef(() => HttpClient)) http:HttpClient, private url: string) {    
        this.http = http;
      }
    
      check(credential: Credentials, url?) {
        url = url ? url : this.url;
    
        return this.http.post(url, credential)
          .pipe(
            //map((response) => { console.log(response.json()); return response.json() }),
            map((response) => { return response }),
            retry(0),
            catchError((error: Response) => { return this.handleError(error) })
          );
      }
    
      isUserExists(user: {username: string}, url?){
    
        url = url ? url : this.url;
    
        return this.http.post(url, user)//, {observe: 'response'})
          .pipe(
            map(response => { return response;}),
            catchError( error => this.handleError(error))
          )
      }
    
      private handleError(error: any) {    
        //console.log("handleError: ", error);
        if (error.status as number === GLOBAL.USER_NOT_FOUND) {      
          return throwError(new NotFoundError(error));
        }
        else if (error.status as number === GLOBAL.UNAUTHENTICATED) {      
          let e = new Unauthorized(error);      
          return throwError(e);
        }
        else {      
          return throwError(new AppError(error));
        }
      }
    }
    

    auth.service.ts 扩展登录服务
    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { LoginService } from '../abstract/login.service';
    //import { Http } from '@angular/http';
    
    @Injectable({
      providedIn: 'root'
    })
    
    export class AuthService extends LoginService {  
    
      constructor(http: HttpClient) { 
        super(http, "http://demo1601932.mockable.io/login-invalid");  
      }
    
      isUserExists(user: {username: string}){
        let url;
    
        if (user.username == "cust@ngapp.com")
          url = "http://demo1601932.mockable.io/user-valid";
        else
          url = "http://demo1601932.mockable.io/user-invalid";
    
        return super.isUserExists(user, url);
      }
    
      check(user){
    
        let url;
    
        if (user.username == "cust@ngapp.com" && user.password == "cust1234")
          url = "https://demo1601932.mockable.io/login-valid";
        else
          url = "https://demo1601932.mockable.io/login-invalid";
    
          return super.check(user, url);
      }
    }
    

    user.service.ts
    import { Injectable, forwardRef, Inject } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { catchError, map, retry } from 'rxjs/operators';
    import { throwError } from 'rxjs';
    import { NotFoundError } from '../errors/notfound-error';
    import { Unauthorized } from '../errors/unauthorized-error';
    import { AppError } from '../errors/app-error';
    import * as GLOBAL from 'globals';
    
    @Injectable({
      providedIn: 'root'
    })
    export class UserService {
    
      private http: HttpClient;  
    
      constructor(@Inject(forwardRef(() => HttpClient)) http:HttpClient, private url: string) {
        this.http = http;    
      }
    
      getAll(url? : string){
    
        url = url ? url : this.url;
    
        return this.http.get(url).pipe(
          map(response => { return response;}),
          catchError( error => this.handleError(error))
        )
      }
    
      getUser(uid, url? : string){
        url = url ? url : this.url;
    
        return this.http.get(url + "/" + uid)
        .pipe(
          retry(2),
          catchError( error => this.handleError(error))
        )
      }
    
      create(user, url? : string){  
        url = url ? url : this.url;
    
        return this.http.put(url, user).pipe(
          map(response => { return response;}),
          catchError( error => this.handleError(error))
        )
      }
    
      update(uid, data, url? : string){
        url = url ? url : this.url;
    
        return this.http.patch(url + '/'+ uid, data, { observe: "response"}).pipe(
          map(response => {
            // console.log('Headers', response.headers.keys());
            // console.log('Body', response.body);
            return response.body;
          }),
          retry(1),
          catchError( error => this.handleError(error))
        )
      }
    
      private handleError(error: any) {    
        //console.log("handleError: ", error);
        if (error.status as number === GLOBAL.USER_NOT_FOUND) {      
          return throwError(new NotFoundError(error));
        }
        else if (error.status as number === GLOBAL.UNAUTHENTICATED) {      
          let e = new Unauthorized(error);      
          return throwError(e);
        }
        else {      
          return throwError(new AppError(error));
        }
      }
    }
    

    客户服务.ts 扩展用户服务
    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { UserService } from '../abstract/user.service';
    
    @Injectable({
      providedIn: 'root'
    })
    
    export class CustomersService extends UserService {
    
      constructor(http: HttpClient) {
        super(http, "http://demo1601932.mockable.io/customers/");
      }
    
      getAll(){
        return super.getAll("http://demo1601932.mockable.io/customers/get");
      }
    
      get(uid){
        return super.getUser(uid, "http://demo1601932.mockable.io/customer/get");
      }
    
      create(user){
        return super.create(user, "http://demo1601932.mockable.io/customers");
      }
    
      update(uid, userData){
        return super.update(uid, userData, "http://demo1601932.mockable.io/customers");
      }
    
      //Admin
      getall4Admin(){
        return super.getAll("https://jsonplaceholder.typicode.com/users");
      }
    
      get4Admin(uid){
        return super.getUser(uid, "https://jsonplaceholder.typicode.com/users");
      }
    }
    

    如果有人想要,我可以发布所有 3 个模块 [admin、customers、app] 模块文件代码。

    最佳答案

    UserService ,您已将服务标记为 @Injectable ,这意味着 Angular 需要知道如何实例化它。它不知道您想为 URL 使用什么。

    如果不想注入(inject) UserService直接进入构造函数,然后简单地删除:

    @Injectable({
      providedIn: 'root'
    })
    

    来自 UserService .

    或者,如果您确实想注入(inject)构造函数,则删除 url来自构造函数的属性,并使用 set 设置它相反,通过将以下代码添加到 UserService
    set url(url: string) {
        this.url = url;
    }
    

    在子类构造函数中,将其设置为 super.url = "http://demo1601932.mockable.io/customers/"

    关于angular - ng build 有效,但不适用于 --prod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112590/

    相关文章:

    c++ - MSVC 放弃带有 "fatal error C1060: compiler is out of heap space"的模板重代码

    Angular 2 Material 输入焦点不起作用

    angular - 未捕获错误 : Unexpected module 'FormsModule' declared by the module 'AppModule' . 请添加 @Pipe/@Directive/@Component 注解

    grails - 从quartz作业访问grails应用程序配置

    java - 将7/2除以3.5,但是为什么在第一种情况下没有显示不兼容错误,而在第二种情况下又为什么不显示.5呢?

    python-3.x - 人脸识别opencv断言失败

    javascript - 重构链式 RxJs 订阅

    angular - NX如何在前端和后端使用接口(interface)库

    asp.net-core - .NET 6 IHubContext 依赖注入(inject)

    c# - 是否可以在应用程序启动时不使用服务定位器来实现依赖注入(inject)?