【发布时间】:2018-07-22 17:12:24
【问题描述】:
最近我拿起了 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]).
为此,我在 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()));
}
}
}
但后来我忽略了更明显的选择,比如忘记@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);
}
}
遗憾的是,我什至没有收到任何关于循环依赖的警告或任何我可以在这里发布的远程有用的东西。这意味着……什么都没有。错误消息是我唯一的来源,我束手无策。
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"
}
}
编辑:我忘了提及,我可以进行一般构建。默认情况下,生产性构建的 aot-compilation 为 true。当我停用它时,它似乎有效,但丑化似乎也坏了,页面也坏了。触发这样的错误:https://github.com/angular/angular-cli/issues/8391
我的项目只有在我使用 ng build 时才有效。
【问题讨论】:
-
从提供者中删除 DataService,因为您手动初始化它
-
它似乎解决了一个问题,但却造成了大量错误。大部分都是直接弹出来的,因为我在component.ts文件里没有合适的值,所以在制作过程中弹出也不足为奇。我会努力清理它们并告诉你它是否有效!到目前为止,感谢您的建议!
-
在陷入 Mosh Hamedani 教程中给出的示例之后,我来到了这里。删除
@Injectable(){}代码块就行了。 -
@VibhorDube 避免像瘟疫一样的所有 Web 开发教程。在他发布它们时,它们中的大多数已经过时了。更糟糕的是,他从不花精力更新它们。
-
@ak.leimrey 遗憾的是,像我这样的大多数学生只有在完成了将近 70-80% 的教程后才知道这一点,此时图书馆的真正功能开始了,并且所有解释的方法都被弃用了显示在视频中。 :(
标签: angular typescript