我有以下拦截器:
"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
这是我从调试器获得的信息:
进入调用堆栈:
有什么想法吗?从昨天开始,我一直在撞墙。 谢谢!
修改
这是一个服务示例:
.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) { }
我得到同样的错误......
答案 0 :(得分:1)
从你的代码:
// Interceptors
.service('ApiInterceptor', ApiInterceptor);
角度拦截器是工厂,你不能使用类。 :
// Interceptors
.factory('ApiInterceptor', somePlainOldFunctionAndNotAClassConstructor);