在(某些服务)上找不到指令注释
No Directive annotation found on (some service)
在浏览了 angular2 的 git 存储库之后,在堆栈溢出中进行了长时间的搜索之后,我需要了解两件事。
1) 第一个是为什么我有这种类型的异常,我的 .ts 文件是这些:
DownloadDocuments.service.ts
import { Injectable } from '@angular/core'
import { Headers,Http,Response } from '@angular/http'
import { Observable } from 'rxjs/Observable'
import { Result } from '../components/downloads/documents/result'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
import 'rxjs/add/observable/throw'
@Injectable()
export class DownloadDocumentsService{
constructor(private http:Http){}
private url='http://someplace/;
getResults(): Observable<Result[]>{
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` :'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
并且服务被注入到这个组件中
DocumentsArea.component.ts
import { Component,OnInit,Input } from '@angular/core'
import { Result } from './documents/result'
import { ResultComponent } from './documents/result.component'
import { DownloadDocumentsService } from '../../services/DownloadDocumentsService.service'
@Component({
selector:'documents-download',
templateUrl:'app/components/downloads/documentsArea.component.html',
styleUrls:['app/components/downloads/documentsArea.component.css'],
directives:[ResultComponent,DownloadDocumentService]
})
export class DocumentsAreaComponent implements OnInit{
documentsList: Result[];
errore: string;
private document:Result;
mode='Observable'
constructor(private downloadDocumentService: DownloadDocumentService){}
getDocuments(){
this.downloadDocumentService.getResults()
.subscribe(
documentsList=>this.documentsList=documentsList,
error => this.errore=<any>error
)
}
ngOnInit(){
this.getDocuments();
}
}
我得到的例外是这个
Error: Uncaught (in promise): No Directive annotation found on DownloadDocumentsService
2) 如何记录 angular 2 可能引发的异常?我是 angular 2 的新手(正如您从代码中看到的,实际上大部分 .ts 文件包含开发人员指南中给出的相同代码,来自 'HTTP client' 一章)但是我收到的错误消息非常通用(我认为),因为我知道这意味着 'DownloadDocumentsService lacks something that makes it a directive'?!?即使这不是这个错误的真正含义,大多数人以非常不同的方式解决了这个问题,让我清楚地知道我不理解这个异常。那么错误的真正含义是什么?拿到手后如何调试?
DownloadDocumentService
服务必须在组件的 providers
属性中指定,而不是在 directives
属性中指定。后者仅适用于指令/组件。
这是一个示例:
directives: [ResultComponent],
providers: [DownloadDocumentService]
为了可用,directives
属性中提供的 class 需要具有 @Directive
或 @Component
装饰器来设置配置元数据。此级别不支持简单 classes。 @Injectable
装饰器不提供类似的东西并且 "only" 能够将依赖项注入 class...
在浏览了 angular2 的 git 存储库之后,在堆栈溢出中进行了长时间的搜索之后,我需要了解两件事。 1) 第一个是为什么我有这种类型的异常,我的 .ts 文件是这些:
DownloadDocuments.service.ts
import { Injectable } from '@angular/core'
import { Headers,Http,Response } from '@angular/http'
import { Observable } from 'rxjs/Observable'
import { Result } from '../components/downloads/documents/result'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
import 'rxjs/add/observable/throw'
@Injectable()
export class DownloadDocumentsService{
constructor(private http:Http){}
private url='http://someplace/;
getResults(): Observable<Result[]>{
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` :'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
并且服务被注入到这个组件中
DocumentsArea.component.ts
import { Component,OnInit,Input } from '@angular/core'
import { Result } from './documents/result'
import { ResultComponent } from './documents/result.component'
import { DownloadDocumentsService } from '../../services/DownloadDocumentsService.service'
@Component({
selector:'documents-download',
templateUrl:'app/components/downloads/documentsArea.component.html',
styleUrls:['app/components/downloads/documentsArea.component.css'],
directives:[ResultComponent,DownloadDocumentService]
})
export class DocumentsAreaComponent implements OnInit{
documentsList: Result[];
errore: string;
private document:Result;
mode='Observable'
constructor(private downloadDocumentService: DownloadDocumentService){}
getDocuments(){
this.downloadDocumentService.getResults()
.subscribe(
documentsList=>this.documentsList=documentsList,
error => this.errore=<any>error
)
}
ngOnInit(){
this.getDocuments();
}
}
我得到的例外是这个
Error: Uncaught (in promise): No Directive annotation found on DownloadDocumentsService
2) 如何记录 angular 2 可能引发的异常?我是 angular 2 的新手(正如您从代码中看到的,实际上大部分 .ts 文件包含开发人员指南中给出的相同代码,来自 'HTTP client' 一章)但是我收到的错误消息非常通用(我认为),因为我知道这意味着 'DownloadDocumentsService lacks something that makes it a directive'?!?即使这不是这个错误的真正含义,大多数人以非常不同的方式解决了这个问题,让我清楚地知道我不理解这个异常。那么错误的真正含义是什么?拿到手后如何调试?
DownloadDocumentService
服务必须在组件的 providers
属性中指定,而不是在 directives
属性中指定。后者仅适用于指令/组件。
这是一个示例:
directives: [ResultComponent],
providers: [DownloadDocumentService]
为了可用,directives
属性中提供的 class 需要具有 @Directive
或 @Component
装饰器来设置配置元数据。此级别不支持简单 classes。 @Injectable
装饰器不提供类似的东西并且 "only" 能够将依赖项注入 class...