简体   繁体   English

服务中注入的服务未定义

[英]Service injected in service is undefined

I have two services, where first is injected in second service, in this way: 我有两个服务,以这种方式将第一个服务注入第二个服务:

rule.service.ts rule.service.ts

@Injectable()
export class RuleService {

    constructor(
        private _resourceService: ResourceService
    ){}

    someMethod(url: string) {
       this._resourceService.getData(url).then((res) => {
          console.log(res);
       }
    }

}

resource.service.ts resource.service.ts

@Injectable()
export class ResourceService {

   constructor(
       http: Http
   ) { }

   public getData(url?: string): Promise<T> {
       //some code
   }
}

called service jQuery :( 称为服务 jQuery :(

private run(input: any, a_parameters: any) {
$("select[name='" + a_parameters[0] + "']").change(function(e: any) {
            return new Promise((resolve) => {
                let array: any[] = [];
                this._resourceService.getData(a_parameters[1]).then(() => {
                    let result: any;
...

but when I trying call someMethod from RuleService , I get this console error: 但是当我尝试从RuleService调用someMethod时,出现以下控制台错误:

EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'getData' of undefined TypeError: Cannot read property 'getData' of undefined at eval (eval at ( http://localhost:8099/app.js:457:2 ), :210:39) at new ZoneAwarePromise (eval at ( http://localhost:8099/polyfills.js:2304:2 ), :695:29) at HTMLSelectElement.eval (eval at ( http://localhost:8099/app.js:457:2 ), :208:20) at HTMLSelectElement.dispatch ( http://cdn.execon.pl/resources/GRM/js/libs/jquery-1.11.0.min.js:3:8066 ) at HTMLSelectElement.r.handle ( http://cdn.execon.pl/resources/GRM/js/libs/jquery-1.11.0.min.js:3:4767 ) at ZoneDelegate.invokeTask (eval at ( http://localhost:8099/polyfills.js:2304:2 ), :363:31) at Object.onInvokeTask (eval at ( http://localhost:8099/vendor.js:101:2 ), :3971:41) at ZoneDelegate.invokeTask (eval at ( http://localhost:8099/polyfills.js:2304:2 ), :362:36) at Zone.runTask (eval at ( http://localhost:8099/polyfills.js:2304:2 ), :166:47) at HTMLSelectElement.ZoneTask.invoke (eval at ( http://lo 例外:未捕获(承诺):TypeError:无法读取未定义的属性'getData'TypeError:无法读取eval的未定义属性'getData'(eval位于( http:// localhost:8099 / app.js:457:2 ) ,:210:39)在新的ZoneAwarePromise(eval在( http:// localhost:8099 / polyfills.js:2304:2 ),:695:29)在HTMLSelectElement.eval(eval在( http:// localhost:8099 /app.js:457:2 ):: 208:20), 网址为HTMLSelectElement.dispatch( http://cdn.execon.pl/resources/GRM/js/libs/jquery-1.11.0.min.js:3: 8066 )在HTMLSelectElement.r.handle( http://cdn.execon.pl/resources/GRM/js/libs/jquery-1.11.0.min.js:3:4767 )在ZoneDelegate.invokeTask(EVAL在( HTTP :// Object:onInvokeTask上的:// localhost:8099 / polyfills.js: 2304:2 ):: 363:31(eval at( http:// localhost:8099 / vendor.js:101:2,: 3971:41 )在ZoneDelegate.invokeTask(EVAL在( HTTP://本地主机:8099 / polyfills.js:2304:2 ),:362:36)在Zone.runTask(EVAL在( HTTP://本地主机:8099 / polyfills.js :2304:2 ),:166:47),网址为HTMLSelectElement.ZoneTask.invoke(eval at( http:// lo calhost:8099/polyfills.js:2304:2 ), :416:38) ErrorHandler.handleError @ core.umd.js?e2a5:3064 next @ core.umd.js?e2a5:8041 schedulerFn @ core.umd.js?e2a5:3689 SafeSubscriber.__tryOrUnsub @ VM86162:223 SafeSubscriber.next @ VM86162:172 Subscriber._next @ VM86162:125 Subscriber.next @ VM86162:89 Subject.next @ VM86159:55 EventEmitter.emit @ core.umd.js?e2a5:3675 NgZone.triggerError @ core.umd.js?e2a5:4040 onHandleError @ core.umd.js?e2a5:4001 ZoneDelegate.handleError @ zone.js?fad3:334 Zone.runGuarded @ zone.js?fad3:142 _loop_1 @ zone.js?fad3:540 drainMicroTaskQueue @ zone.js?fad3:549 ZoneTask.invoke @ zone.js?fad3:420 ListPicker._handleMouseUp @ about:blank:540 calhost:8099 / polyfills.js:2304:2 ):416:38)ErrorHandler.handleError @ core.umd.js?e2a5:3064 next @ core.umd.js?e2a5:8041 schedulerFn @ core.umd.js? e2a5:3689 SafeSubscriber .__ tryOrUnsub @ VM86162:223 SafeSubscriber.next @ VM86162:172 Subscriber._next @ VM86162:125 Subscriber.next @ VM86162:89 Subject.next @ VM86159:55 EventEmitter.emit @ core.umd.js?e2a5: 3675 ​​NgZone.triggerError @ core.umd.js?e2a5:4040 onHandleError @ core.umd.js?e2a5:4001 ZoneDelegate.handleError @ zone.js?fad3:334 Zone.runGuarded @ zone.js?fad3:142 _loop_1 @ zone .js?fad3:540rainMicroTaskQueue @ zone.js?fad3:549 ZoneTask.invoke @ zone.js?fad3:420 ListPicker._handleMouseUp @关于:空白:540

Anyone can tell me, what I do wrong? 谁能告诉我,我做错了什么? How can I correct implement service in service? 如何在服务中正确实施服务?

You need to use arrow function to retain this 您需要使用箭头功能来保留this

$("select[name='" + a_parameters[0] + "']")
   .change((e: any) => { // <== arrow function instead of FE(function expression)
        return new Promise((resolve) => {
            let array: any[] = [];
            this._resourceService.getData(a_parameters[1]).then(() => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM