Dee*_*eej 2 angular2-services angular2-http angular2-di angular
我正在尝试在Angular 2中创建一个通用数据服务,我遇到了一个奇怪的错误.本质上,我正在创建一个HTTP服务,其方法包含api url的一部分,以便我可以将它用于多种情况.例如,我想传递'projects /'并加入'api /'来获取'api/projects /'然后我可以在http调用中使用它.
我目前的dataService.ts:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';
import { Configuration } from '../app.constants';
@Injectable()
export class DataService {
private actionUrl: string;
private headers: Headers;
constructor(private _http: Http, private _configuration: Configuration, public action: string) {
this.actionUrl = _configuration.ApiUrl
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
this.headers.append('Accept', 'application/json');
}
public GetAll (action: string): Observable<any> {
return this._http.get(this.actionUrl + action).map((response: Response) => <any>response.json());
}
public GetSingle (action: string, id: number): Observable<Response> {
return this._http.get(this.actionUrl + action + id).map(res => res.json());
}
public Add (action: string, itemToAdd: any): Observable<Response> {
var toAdd = JSON.stringify(itemToAdd);
return this._http.post(this.actionUrl + action, toAdd, { headers: this.headers }).map(res => res.json());
}
public Update (action: string, id: number, itemToUpdate: any): Observable<Response> {
return this._http
.put(this.actionUrl + id, JSON.stringify(itemToUpdate), { headers: this.headers })
.map(res => res.json());
}
public Delete (action: string, id: number): Observable<Response> {
return this._http.delete(this.actionUrl + id);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用'projects'作为参数来调用GetAll的组件:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../services/dataService';
@Component({
selector: 'project-detail',
templateUrl: 'app/project/project-detail.component.html'
})
export class ProjectDetailComponent implements OnInit{
projects: Object[];
constructor(private _dataService: DataService) {}
getProjects(): void {
this._dataService.GetAll('projects').subscribe(projects => this.projects = projects);
}
ngOnInit(): void {
this.getProjects();
}
}
Run Code Online (Sandbox Code Playgroud)
我确实在app.module.ts中添加了作为提供程序的服务.还有一点值得注意的是,我在这里展示的组件是名为HomeComponent的组件的子组件,它位于AppComponent中.
这就是HomeComponent的样子:
import { Component, OnInit } from '@angular/core';
import { ProjectDetailComponent } from '../project/project-detail.component';
@Component({
selector: 'home',
templateUrl: '<project-detail></project-detail>'
})
export class HomeComponent {}
Run Code Online (Sandbox Code Playgroud)
错误:
EXCEPTION: Uncaught (in promise): Error: Error in app/home/home.component.html:2:0 caused by: No provider for String!
Run Code Online (Sandbox Code Playgroud)
我知道它正在抱怨我传入GetCall的字符串,但我不知道为什么.
任何帮助,将不胜感激!
你不能只是注入任意东西.您尝试注射的任何物质都需要配置为可注射.在大多数情况下,这意味着只需将类添加到providers列表中.在字符串的情况下,它并不那么简单.我们需要做一些事情.
@Inject它进入目标.import { OpaqueToken } from '@angular/core';
// 1. Create token
export const ACTION = new OpaqueToken('app.action');
// 2. Configure the `providers` (Maybe in the AppModule)
providers: [
{ provide: ACTION, useValue: 'the value you want'
]
import { Inject } from '@angular/core';
export class DataService {
// 3. Injection target
constructor(@Inject(ACTION) action: string) {}
}
Run Code Online (Sandbox Code Playgroud)
1 - 请参阅依赖注入令牌
现在,使用Angular 4,我们应该使用InjectionToken而不是OpaqueToken
| 归档时间: |
|
| 查看次数: |
4545 次 |
| 最近记录: |