TypeError定义/访问AngularJS / TypeScript / ES6中的拦截器

时间:2016-01-19 20:26:11

标签: javascript angularjs typescript ecmascript-6

我有以下拦截器:

"use strict";

import { IApiService } from '../services/api';
import { ISessionService } from '../services/session';
import { IRedirectService } from '../services/redirect';

export default class ApiInterceptor {

  constructor(
    private $q: ng.IQService,
    private ApiService: IApiService,
    private SessionService: ISessionService,
    private RedirectService: IRedirectService) { }

  ...

}

这就是它如何设置为服务:

import ApiInterceptor          from './interceptors/api';
...

export default angular.module('app.core', [
    'toastr', 'js-data'
  ])

  // Constants
  .constant('config', config)

  // Config
  .config(DataConfig)

  // Filters
  ...

  // Interceptors
  .service('ApiInterceptor', ApiInterceptor);

然后在我的app.instance中,我将app.core设为dep:

'use strict';

import '../app.core/module';
...

export default angular.module('app.instance', [
  ...
  'app.core',
  ...]);

并在此处使用其代码:

'use strict';

import '../../app.core/module';
...

export default function ConfigApp($httpProvider, $locationProvider, $analyticsProvider): void {
  ...
  $httpProvider.interceptors.push('ApiInterceptor');
  ...
}

然后我收到此错误:

Uncaught TypeError: Cannot read property 'prototype' of undefined

这是我从调试器获得的信息:

enter image description here

进入调用堆栈:

enter image description here

有什么想法吗?从昨天开始,我一直在撞墙。 谢谢!

修改

这是一个服务示例:

.service('LogService', LogService)

这是实施:

"use strict";

export interface ILogService {
  debug(...args: any[]): void;
  info(...args: any[]): void;
  warn(...args: any[]): void;
  error(...args: any[]): void;
  deprecated(...args: any[]): void;
  enableDebug(flag: boolean): void;
  enableConsole(flag: boolean): void;
}

/**
 * Provides logging support.
 */
export default class LogService implements ILogService {

  static debugEnabled: boolean = false;
  static consoleEnabled: boolean = true;

  constructor(
    private $log: any,
    private $window: ng.IWindowService) { }

我得到同样的错误......

1 个答案:

答案 0 :(得分:1)

从你的代码:

// Interceptors
  .service('ApiInterceptor', ApiInterceptor);

角度拦截器是工厂,你不能使用。 :

// Interceptors
  .factory('ApiInterceptor', somePlainOldFunctionAndNotAClassConstructor);