我在Angular 5中有一个小应用程序,当我尝试运行build prod ng build --prod时,我收到此错误:
Time: 4739ms
chunk {0} styles.c716146007b500deaef3.bundle.css (styles) 175 kB [initial] [rendered]
chunk {1} polyfills.997d8cc03812de50ae67.bundle.js (polyfills) 84 bytes [initial] [rendered]
chunk {2} main.ee32620ecd1edff94184.bundle.js (main) 84 bytes [initial] [rendered]
chunk {3} inline.318b50c57b4eba3d437b.bundle.js (inline) 796 bytes [entry] [rendered]
ERROR in : Can't resolve all parameters for DataService in .../src/app/services/data.service.ts: (?, [object Object]).
对我来说,这是一个很大的惊喜,因为正常,默认的方式是应用程序正确运行。 我调查了其他类似的错误,它似乎来自依赖。
但最大的问题是,如何在正常模式下运行正常,并在运行构建时给我这个错误?!
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { AppError } from '../common/app-error';
import { NotFoundError } from '../common/not-found-error';
import { BadInput } from '../common/bad-input';
@Injectable()
export class DataService {
constructor(public url: string, private http: HttpClient) { }
getAll() {
return this.http
.get(this.url)
.catch(this.handleError);
}
create(resource) {
return this.http
.post(this.url, JSON.stringify(resource))
.catch(this.handleError)
}
update(resource) {
return this.http
.patch(`${this.url}/${resource.id}`, JSON.stringify({ isRead: true}))
.catch(this.handleError);
}
delete(id) {
return this.http
.delete(`${this.url}/${id}`)
.catch(this.handleError);
}
private handleError(error: Response) {
if(error.status === 400) {
return Observable.throw( new BadInput(error));
}
if(error.status === 404) {
return Observable.throw(new NotFoundError());
}
return Observable.throw(new AppError(error))
}
}
使用:
import { DataService } from './data.service';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class GithubFollowersService extends DataService {
constructor(http: HttpClient) {
super('https://api.github.com/users/mosh-hamedani/followers', http);
}
}
..和:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DataService } from './data.service';
@Injectable()
export class PostService extends DataService {
// private url: string = 'https://jsonplaceholder.typicode.com/posts';
constructor(http: HttpClient) {
super('https://jsonplaceholder.typicode.com/posts', http);
}
}
答案 0 :(得分:1)
我认为问题可能是由于url
中注入了constructor() {}
。这可能会提供一个修复:
data.service.ts
import { Router } from '@angular/router';
constructor(private: router: Router, private http: HttpClient) {}
在调用this.url
的任何地方,将其替换为this.router.url
答案 1 :(得分:0)
如果你的角度版本是4或更高,而不是使用HttpClient,请尝试使用:
import { Http } from '@angular/http';
答案 2 :(得分:0)
我解决了这个问题:
我从DataService中删除@Injectable装饰器
@Injectable()//删除它 导出类DataService { ... }
从app.modules.ts,我也从提供者中删除了DataService:[]
提供商[ ... DataService //已删除 ... ]
构造函数没有可注入的参数(url:string)。这解决了我的问题。
感谢所有想法!!!