TypeError: Cannot call a class as a function
Working on an Angular 1.x app, using ES6, an Angular Linter, and Babel for transpiling. I am receiving this error: "TypeError: Cannot call a class as a function" in the console, though the html loads just fine.
TypeError: Cannot call a class as a function
at _classCallCheck (bundle.js:97664)
at Object.loginNotifyService (bundle.js:97670)
at Object.invoke (bundle.js:23052)
at Object.enforcedReturnValue [as $get] (bundle.js:22885)
at Object.invoke (bundle.js:23052)
at bundle.js:22844
at getService (bundle.js:22993)
at injectionArgs (bundle.js:23018)
at Object.invoke (bundle.js:23044)
at $controllerInit (bundle.js:29012) "<div ui-view="" class="ng-scope">"
Best I can tell, syntax is correct. My best guess is Babel transpiling to ES5, specifically this:
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
Here is the source JS:
'use strict';
class loginNotifyService {
constructor (notify) {
this.loginNotifyService = notify;
}
info (message, config) {
config = config || {};
config.message = message;
config.classes = 'alert alert-info ' + (config.classes || '');
return this.loginNotifyService(config);
}
warn (message, config) {
config = config || {};
config.message = message;
config.classes = 'alert alert-warning ' + (config.classes || '');
return this.loginNotifyService(config);
}
error (message, config) {
config = config || {};
config.message = message;
config.classes = 'alert alert-danger ' + (config.classes || '');
return this.loginNotifyService(config);
}
success (message, config) {
config = config || {};
config.message = message;
config.classes = 'alert alert-success ' + (config.classes || '');
return this.loginNotifyService(config);
}
notify (config) {
return this.loginNotifyService(config);
}
closeAll () {
return this.loginNotifyService.closeAll();
}
}
// loginNotifyService.$inject = ['notify'];
/* @ngInject */
export default loginNotifyService;
Here is the Controller that the loginNotifyService interacts with:
'use strict';
class loginController {
constructor ($state, loginNotifyService, loginService) {
this.$state = $state;
this.loginNotifyService = loginNotifyService;
this.loginService = loginService;
this.loginInProgress = false;
}
login () {
this.loginNotifyService.closeAll();
this.loginInProgress = true;
this.loginService.login(this.email, this.password).then(
() => {
this.loginInProgress = false;
this.$state.go('dashboard');
},
(error) => {
this.loginInProgress = false;
this.showErrors(error);
}
);
}
showErrors (error) {
this.errors = error;
this.loginNotifyService.error(error);
}
}
// loginController.$inject = ['$state', 'loginNotifyService', 'loginService'];
/* @ngInject */
export default loginController;
LMK if further clarification or info needed, and thank you for any advice.
I was able to resolve this issue by changing the factory to a service. It was initially set as a factory due to a linting rule that I had set from this linter:
https://github.com/Gillespie59/eslint-plugin-angular
Here is the specific rule:
https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-service-method.md
(I had to disable this specific rule in order to make change from factory to service)
The structure of the loginNotifyService code needs to be a service in order to work correctly (as it is written currently). I was able to get a clearer understanding of the difference between the two by reading this post:
AngularJS : Factory and Service?
EXAMPLE:
angular
.module('commonModule', [
uiRouter,
cgNotify,
ngAnimate,
ngMaterial,
ngMessages,
ngSanitize,
ngAria,
'navRoute',
'mdTheme',
])
// ALL OF THE SERVICES BELOW WERE PREVIOUSLY FACTORIES.
// CHANGING "loginNotifyService" TO A SERVICE INSTEAD,
// FIXED THE "TypeError: Cannot call a class a function" ERROR!
.service('authService', authService)
.value('loginConfigService', loginConfigService)
.service('loginNotifyService', loginNotifyService)
.service('loginService', loginService)
.service('resetPassService', resetPassService)
.component('login', loginComponent)
.component('resetPass', resetPassComponent);
Also, thank you @Rhoden for your response and insights!
This seems to be an 'understanding' error, AngularJS will not instantiate a 'transpiled'(compiled) service class unless it 'see' that it is a es6 class (as shown by the invoke code below).
Compiled classes by babel aren't classes, but functions acting as classes, so babel creates some checks (this function here: _classCallCheck
) to prevent a class to be called as function (as they only should be 'called' with new
keyword).
AngularJS on the invoke function uses this code:
function isClass(func) {
// Support: IE 9-11 only
// IE 9-11 do not support classes and IE9 leaks with the code below.
if (msie || typeof func !== 'function') {
return false;
}
var result = func.$$ngIsClass;
if (!isBoolean(result)) {
result = func.$$ngIsClass = /^classb/.test(stringifyFn(func));
}
return result;
}
Which fails to detect babel compiled classes. So, this validation also fails:
if (!isClass(fn)) {
// http://jsperf.com/angularjs-invoke-apply-vs-switch
// #5388
return fn.apply(self, args);
} else {
args.unshift(null);
return new (Function.prototype.bind.apply(fn, args))();
}
And you get a "TypeError: Cannot call a class as a function"
error.
Edit : i was giving a look again at that code and you probably can declare a class like this to make this work:
class Foo {
static get $$ngIsClass(){return true;}
}
This will force isClass
to return true, and it will be called with the new
keyword. (if you could test and confirm that it works i would be grateful).
上一篇: 具有多个控制器的角度共享模型
下一篇: TypeError:不能作为函数调用类