javascript - 类型错误 : Cannot call a class as a function - ES6 - Angular1. x - Webpack

标签 javascript angularjs authentication notifications babeljs

正在开发 Angular 1.x 应用程序,使用 ES6、Angular Linter 和 Babel 进行转译。我在控制台中收到此错误:“TypeError:无法将类作为函数调用”,尽管 html 加载得很好。

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">"

据我所知,语法是正确的。我最好的猜测是 Babel 转译为 ES5,具体来说:

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

这是源代码:

    '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;

这是 loginNotifyService 与之交互的 Controller :

'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 如果需要进一步的说明或信息,并感谢您的任何建议。

最佳答案

我能够通过将工厂更改为服务来解决此问题。 由于我从这个 linter 中设置了 linting 规则,它最初被设置为工厂:

https://github.com/Gillespie59/eslint-plugin-angular

具体规则如下:

https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-service-method.md
(我必须禁用此特定规则才能从工厂更改为服务)

loginNotifyService 代码的结构需要是一个服务才能正常工作(如当前所写)。通过阅读这篇文章,我能够更清楚地了解两者之间的区别:

AngularJS : Factory and Service?

示例:

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);

另外,感谢@Rhoden 的回复和见解!

关于javascript - 类型错误 : Cannot call a class as a function - ES6 - Angular1. x - Webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47104102/

相关文章:

javascript - Highcharts 悬停错误 - 有视频演示

javascript - Angularjs 和 Chart.js 最小示例

android - Linkedin 在 Android 中的集成错误 : not showing authorization screen in linkedin app

Laravel - 套接字 - Ratchet - api 身份验证

facebook - 如何安全地将 Facebook Id 从客户端传递到服务器

javascript - z-index 没有将 div 定位在另一个之上

javascript - 当我点击任意位置时如何移动谷歌地图中的标记

javascript - Node.js 根据其值返回对象键

javascript - 获取元素 ng 类 AngularJS

javascript - ng-table 不显示来自 http get 的数据