Angular4引用地图操作符内的服务
我创建了一个服务:Angular4引用地图操作符内的服务,angular,typescript,Angular,Typescript,我创建了一个服务: @Injectable() export class AppService { constructor(private _convertor: Convertor) foo() { a = ["1", "2", "3"] return a.map(this.blah) } blah(s: string): number { return this_.convertor.parseInt(s) } } 然而
@Injectable()
export class AppService {
constructor(private _convertor: Convertor)
foo() {
a = ["1", "2", "3"]
return a.map(this.blah)
}
blah(s: string): number {
return this_.convertor.parseInt(s)
}
}
然而,它一直说,这个
没有定义。我用的替换了我的地图
,效果很好。我还试着使用\uu.map
,这给了我同样的结果。
知道如何指定映射它应该使用什么吗?当前上下文由于回调函数而丢失,从中,您可以通过向映射函数传递第二个参数来保留当前上下文。所以
@Injectable()
export class AppService {
constructor(private _convertor: Convertor)
foo() {
a = ["1", "2", "3"]
return a.map(this.blah, this)
}
blah(s: string): number {
return this._convertor.parseInt(s)
}
}
而且它也是this.\u convertor.parseInt(s)
,但不是this.\u convertor.parseInt(s)
试试这个
import { Injectable } from "@angular/core/src/core";
import { Convertor } from "./Convertor"
export class AppService {
constructor(private _convertor: Convertor)
{
}
foo() {
let a = ["1", "2", "3"]
return a.map(this.blah)
}
blah(s: string): number {
return this._convertor.parseInt(s)
}
}
可能重复的