Angular 5 : Can't resolve all parameters for Service

标签 angular typescript

最近我选择了 Angular 5,我的任务是创建一个演示应用程序。

Ng serve 工作得非常好,甚至 ng build 也没有给我任何问题。我可以不断测试我的应用程序是否在本地开发中工作,但也可以在我的本地 tomcat 服务器上工作(使用 XAMPP)。

但是当我想使用生产版本时,一切都变得很糟糕。

ERROR in : Can't resolve all parameters for DataService in C:/Users/ak/Documents/angular5_project/src/app/services/data.service.ts: (?, [object Object]).

为此,我搜索了多个 git 存储库和 stackoverflow 上的其他问题。

起初,我以为我错过了一些基本的东西。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { NotFoundError } from 'app/common/not-found-error';
import { BadInput } from 'app/common/bad-input';
import { AppError } from 'app/common/app-error';

@Injectable()
export class DataService {

  constructor(private url: string, private http: HttpClient ) {
  }

  getAll() {
    return this.http.get(this.url)
      .catch(this.handleError);
  }

  get(id) {
    return this.http.get(this.url + '/' + id)
    .catch(this.handleError);
  }

  create(resource) {
    //   return Observable.throw(new AppError());
    return this.http.post(this.url, JSON.stringify(resource))
      .catch(this.handleError);
  }

  update(resource) {
    return this.http.put(this.url + '/' + resource.id,
      JSON.stringify(resource))
      .catch(this.handleError);
  }

  delete(id) {
    return this.http.delete(this.url + '/' + id)
      .catch(this.handleError);
  }

  private handleError(error: Response) {
    if (error.status === 404) {
      return Observable.throw(new NotFoundError());
    } else if (error.status === 400) {
      return Observable.throw(new BadInput(error.json()));
    } else {
      return Observable.throw(new AppError(error.json()));
    }
  }

}

但后来我忽略了更明显的选择,比如忘记@Injectable 注释或忘记在 app.module.ts 中注册服务

...
import { UserDataService } from 'app/services/user-data.service';
import { DataService } from 'app/services/data.service';
...
 providers: [
    MDBSpinningPreloader,
    AuthGuard,
    AdminAuthGuard,
    AuthService,
    UserDataService,
    DataService,
    EquipmentDataService,
    EquipmentAlarmDataService,
...
  ],

经常提到循环依赖的问题,但据我所见,它从来不是基础服务。我有几个从 DataService 继承的服务,比如一个简单地从我的 Spring 后端返回所有可用用户列表的用户服务(只是一组虚拟数据):

import { Injectable } from '@angular/core';
import { DataService } from 'app/services/data.service';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UserDataService extends DataService {

  constructor(http: HttpClient) {
    super('http://localhost:8080/users', http);

   }
}

遗憾的是,我什至没有收到任何关于循环依赖的警告或任何我可以在这里发布的远程有用信息。这意味着……什么都没有。错误消息是我唯一的来源,我束手无策。

包.json

{
  "name": "quickstart-angular5",
  "version": "5.0.5",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build:free": "ngm build -p src/app/typescripts/free --clean && gulp npmFree && gulp startFree && gulp onlyFree",
    "build:pro": "ngm build -p src/app/typescripts/pro --clean && gulp only-pro && gulp startPro",
    "build:all": "npm run build:free && npm run build:pro",
    "aot:build": "ng build --prod --sm=false --aot=true --output-path=dist",
    "pre-commit": "ng lint"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.2",
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-beta.5",
    "angular2-jwt": "^0.2.3",
    "chart.js": "2.5.x",
    "classlist.js": "1.1.x",
    "core-js": "2.4.x",
    "del": "3.0.x",
    "easy-pie-chart": "2.1.x",
    "font-awesome": "4.7.x",
    "gulp": "^3.9.1",
    "gulp-rename": "1.2.x",
    "gulp-run": "1.7.x",
    "hammerjs": "2.0.x",
    "ng-html-util": "1.0.x",
    "ngm-cli": "0.5.x",
    "rxjs": "^5.5.2",
    "screenfull": "3.3.x",
    "smoothscroll-polyfill": "0.3.x",
    "web-animations-js": "2.3.x",
    "zone.js": "0.8.x"
  },
  "devDependencies": {
    "@angular/cli": "^1.6.8",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.85",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.5.0",
    "uglify-es": "3.3.8",
    "webpack": "3.x"
  }
}

编辑:我忘了说,我可以进行一般构建。默认情况下,生产构建具有 aot-compilation true。当我停用它时,它似乎有效,但丑化似乎也被破坏了,页面也被破坏了。触发这样的错误:https://github.com/angular/angular-cli/issues/8391

我的项目只有在我只使用 ng build 时才能工作。

最佳答案

您的DataService 服务有两个构造函数参数,urlhttphttp是可以解析的,因为它被HttpClientModule注册了,但是url参数没有注册到依赖注入(inject)容器中。这就是您收到错误的原因。

我猜你总是允许将特定类(例如 UserDataService)注入(inject)到你的组件中,而不是 DataService 实例。

因此,您应该将 DataService 重构为一个抽象的 DataServiceBase 类,并移除 @Injectable() 装饰器。

按照 yurzui 的建议,从提供者列表中删除 DataService

关于 Angular 5 : Can't resolve all parameters for Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748066/

相关文章:

javascript - 如何在调整窗口大小时保持 chartjs/ng2-charts 渐变?

javascript - 异步函数返回 promise ,但调用函数未解析

json - 无法将数据绑定(bind)到 Angular Material 数据表

javascript - TypeScript:如何在字符串数组上使用 Array.includes 可能未定义的值(可选链接)

从 Typescript 文件导入时,Javascript 代码在 react-scripts 构建中获取 "Attempted import error:"

javascript - 如何在浏览器中运行包含typscript转编译的js文件的导入语句?

typescript - 属性 'fonts' 在类型 'Document' 上不存在?

javascript - 在响应式(Reactive)表单中使用 formArray 时获取未定义的 "TypeError: Cannot read property ' 控件

javascript - 使用 ng-template 进行 ng-select 时,清除图标 'x' 丢失

angular - 如何在下拉列表中将标签设置为空字符串?