在服务A中,我需要使用管道P。此管道P需要服务B才能工作。到目前为止,我所做的是这样的:
我的P管道定义
export class PPipe implements PipeTransform {
constructor(private aService: AService) {}
transform(value:number) : string {
return number.toString();
}
}
我如何在服务中使用它B
@Injectable()
export class BService {
pPipe = new PPipe();
myFn() {
const nbToStr = pPipe.transform(69);
}
}
但是在构建时出现错误:Expected 1 arguments, but got 0.
。
每次我想使用PPipe实例时都需要传递它吗?如果是这样,如何从HTML模板执行它?
感谢您的帮助
您需要注入管道。如果您new
自己创建东西,Angulars DI将无法进行交互。
export class BService {
constructor(private pPipe:PPipe) {}
这样,Angular会创建一个PPipe
实例并将依赖项传递给其构造函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句