【问题标题】:Service injected in service is undefined服务中注入的服务未定义
【发布时间】:2017-08-08 04:04:04
【问题描述】:

我有两个服务,第一个被注入到第二个服务中,这样:

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

@Injectable()
export class ResourceService {

   constructor(
       http: Http
   ) { }

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

调用服务 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;
...

但是当我尝试从 RuleService 调用 someMethod 时,我得到了这个控制台错误:

异常:未捕获(承诺中):TypeError:无法读取属性 未定义类型错误的“getData”:无法读取属性“getData” 不明确的 在评估(评估在(http://localhost:8099/app.js:457:2),:210:39) 在新的 ZoneAwarePromise (eval at (http://localhost:8099/polyfills.js:2304:2), :695:29) 在 HTMLSelectElement.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 at (http://localhost:8099/polyfills.js:2304:2), :363:31) 在 Object.onInvokeTask (评估在(http://localhost:8099/vendor.js:101:2),:3971:41) 在 ZoneDelegate.invokeTask (eval at (http://localhost:8099/polyfills.js:2304:2), :362:36) 在 Zone.runTask (eval at (http://localhost:8099/polyfills.js:2304:2), :166:47) 在 HTMLSelectElement.ZoneTask.invoke(评估在 (http://localhost:8099/polyfills.js:2304:2), :416:38) ErrorHandler.handleError@core.umd.js?e2a5:3064 下一个@ 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 @ 关于:空白:540

谁能告诉我,我做错了什么?如何在服务中更正实施服务?

【问题讨论】:

  • 请在您呼叫的地方添加代码someMethod
  • 编辑:调用方法
  • 谢谢 :) 然后我看到你失去了上下文
  • 非常感谢!是工作。我可以相信我犯了那个错误。 ;)

标签: angular typescript angular2-services


【解决方案1】:

你需要使用箭头函数来保留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(() => {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2014-05-07
    • 1970-01-01
    • 2019-08-09
    • 2016-09-02
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多