角度图书馆 - 类型&#39;主题<任何[]>&#39;不能分配类型&#39;主题<任何[]>&#39;

时间:2017-12-14 12:26:12

标签: dependency-injection angular-services angular5 angular-library

我一直在关注this guide创建一个自定义角度库,但我碰到了一个问题而非奇怪的问题。

练习背后的想法很简单,为特定的功能(在这种情况下是一个人员目录)创建一个框架,开发人员可以在其中安装它并提供实现我们界面的自定义数据源。

此界面如下

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并创建一个新的&#34;测试&#34;应用程序,所以我可以使用npm install导入模块(所有这些步骤按照博客文章)。到目前为止这么好,安装模块没有错误。

测试应用程序只是我在构建库时使用的完全复制品。相同的AppServices继承ISDDataService,提供服务的方式相同等等。我试着建立它,这一切都变得很糟糕。这个错误不会更奇怪

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[]>'"怎么样才有意义!! ??

我在这里和那里做了一些改变,但没有任何作用

请注意,此问题不仅适用于精简程序属性。如果我删除它,它将级联到函数等等。

我开始认为这种方法可能不是正确的......在这里我觉得它很干净

无论如何,如果有人能伸出援手将非常感激

0 个答案:

没有答案