Angular 5:无法解析Service的所有参数

ak.*_*rey 22 typescript angular

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

Ng服务工作得很好,甚至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]).
Run Code Online (Sandbox Code Playgroud)

我在stackoverflow上搜索了几个git存储库和其他问题.

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

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()));
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

但后来我忽略了更明显的选择,比如忘记@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,
...
  ],
Run Code Online (Sandbox Code Playgroud)

通常,提到循环依赖的问题,但从我所看到的,它从来不是基本服务.我有几个从DataService继承的服务,就像一个用户服务,它只是从我的Spring Backend返回一个包含所有可用用户的列表(只是一组虚拟数据):

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);

   }
}
Run Code Online (Sandbox Code Playgroud)

可悲的是,我甚至没有得到任何关于循环依赖的警告或任何远程有用的警告我可以在这里发布.这意味着什么......没有.错误信息是我唯一的来源,我在我的智慧结束.

的package.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

我的项目仅在我仅使用ng build时才有效.

zgu*_*gue 23

您的DataService服务有两个构造函数参数,urlhttp.http可以解析,因为它是由注册的HttpClientModule,但url参数未在依赖注入容器中注册.这就是你得到错误的原因.

我想你总是让一个特定的类,例如UserDataService,注入你的组件,而不是一个DataService实例.

所以你应该重构DataService一个抽象DataServiceBase类并删除@Injectable()装饰器.

正如yurzui建议的那样,DataService从提供商列表中删除.


Bor*_*nov 10

该错误表明构造函数中的第一个参数导致了问题.它应该是因为它是一个简单的字符串.尝试像这样注入它:

@Injectable()
export class DataService {

constructor(@Inject('API_BASE_URL') private url: string, private http: HttpClient) { // ... }
Run Code Online (Sandbox Code Playgroud)

在您的提供商:

providers: [DataService, {provide: 'API_BASE_URL', useValue: 'http://localhost:8080/users'}]
Run Code Online (Sandbox Code Playgroud)