我一直在按照本指南创建一个自定义角度库,但我遇到了一个相当奇怪的问题。
练习背后的想法很简单,为特定功能(在本例中为人员目录)创建一个框架,开发人员可以在其中 npm 安装它并提供实现我们接口的自定义数据源。
这个界面如下
export interface ISDDataService {
refiners: Subject<Refiner[]>;
search(queryTxt: string): Observable<Staff[]>;
refine(refiner: Refiner): Observable<Staff[]>;
}
在库项目中,这个骨架是根模块引用的一个模块,它被设置为提供继承ISDDataService的数据源
@Injectable()
export class AppService implements ISDDataService {
refiners: Subject<Refiner[]>;
staff: Staff[] = [];
constructor() {
this.staff.push(<Staff>{name: 'John Doe', role: 'xyz', group: 'A Team', image: ''});
this.staff.push(<Staff>{name: 'Jane Doe', role: 'xyz', group: 'B Team', image: ''});
}
search(queryTxt: string): Observable<Staff[]> {
return Observable.of(this.staff).map(o => this.staff);
}
refine(refiner: Refiner): Observable<Staff[]> {
return ;
}
}
这就是提供服务的方式(app.module.ts)
providers: [
{
provide: 'ISDDataService',
useClass: AppService
}
],
骨架模块还有使用数据源查询数据的结果组件
@Component({
selector: 'app-search-results',
templateUrl: './search-results.component.html',
styleUrls: ['./search-results.component.css']
})
export class SearchResultsComponent implements OnInit {
private results: Observable<Staff[]>;
private searchField: FormControl;
constructor(@Inject('ISDDataService') private service: ISDDataService) { }
ngOnInit() {
this.searchField = new FormControl();
this.results = this.searchField.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.switchMap( term => this.service.search(term));
}
}
这个设置就像一个魅力,所以继续打包,创建一个 tarball 并创建一个新的“测试”应用程序,这样我就可以使用 npm install 导入模块(所有这些步骤都按照博客文章)。到目前为止一切顺利,安装模块没有错误。
测试应用程序只是我在构建库时使用的完全复制品。继承 ISDDataService 的相同 AppServices,提供服务的相同方式等。我尝试构建它,但一切都陷入困境。错误再奇怪不过了
ERROR in src/app/app.service.ts(9,14): error TS2420: Class 'AppService' incorrectly implements interface 'ISDDataService'.
Types of property 'refiners' are incompatible.
Type 'Subject<Refiner[]>' is not assignable to type 'Subject<Refiner[]>'. Two different types with this name exist, but they are unrelated.
Types of property 'lift' are incompatible.
Type '<R>(operator: Operator<Refiner[], R>) => Observable<R>' is not assignable to type '<R>(operator: Operator<Refiner[], R>) => Observable<R>'. Two different types with this name exist, but they are unrelated.
Types of parameters 'operator' and 'operator' are incompatible.
Type 'Operator<Refiner[], R>' is not assignable to type 'Operator<Refiner[], R>'. Two different types with this name exist, but they are unrelated.
Types of property 'call' are incompatible.
Type '(subscriber: Subscriber<R>, source: any) => TeardownLogic' is not assignable to type '(subscriber: Subscriber<R>, source: any) => TeardownLogic'. Two different types with this name exist, but they are unrelated.
Types of parameters 'subscriber' and 'subscriber' are incompatible.
Type 'Subscriber<R>' is not assignable to type 'Subscriber<R>'. Two different types with this name exist,
but they are unrelated.
Property 'isStopped' is protected but type 'Subscriber<T>' is not a class derived from 'Subscriber<T>'.
这样的事情有什么"Subject<Refiner[]>' is not assignable to type 'Subject<Refiner[]>'"
意义!!???
我在这里和那里做了一些改变,但没有任何效果
请注意,此问题不仅适用于炼油器属性。如果我删除它,它会级联到函数等等。
我开始认为这种方法可能不是正确的方法......在这里我认为它很干净
无论如何,如果有人能伸出援手,将不胜感激