为什么我不能转换我的js文件?

时间:2016-07-15 05:53:15

标签: javascript gulp ecmascript-6 babeljs grunt-babel

我有一个ES6项目。我想将文件转换为ES5。我已经创造了一个gulp任务。这是我使用babel的任务:

gulp.task('transpile' , function() {
  console.log('Transpile js ');
    return gulp.src(['app/**/*.js'])
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(ngAnnotate())
    .pipe(uglify().on('error', function(e){
        console.log(e);
        }))
    .pipe(gulp.dest('target/'));
});

当我运行任务时,我有一个错误:

events.js:141
      throw er; // Unhandled 'error' event
      ^
SyntaxError: /Users/me/myapp/target/tmp/src/content.components/login/login.controller.js: Unexpected token (20:16)
  18 |     }
  19 | 
> 20 |     successAuth = (result) => {
     |                 ^
  21 |         this.$rootScope.$broadcast('authenticationSuccess');
  22 |         this.$location.path('/');
  23 |     };
    at Parser.pp.raise (/Users/me/node_modules/babylon/lib/parser/location.js:22:13)

我的控制器出现问题:

class LoginController {

    constructor($rootScope, $location, AuthService) {
        this.AuthService = AuthService;
        this.$rootScope = $rootScope;
        this.$location = $location;
    }

    login() {
        this.loading = true;
        this.AuthService.login({
            username: this.username,
            password: this.password,
            rememberMe: this.rememberMe
        }).then(this.successAuth, this.failedAuth);
    }

    successAuth = (result) => {
        this.$rootScope.$broadcast('authenticationSuccess');
        this.$location.path('/');
    };
    failedAuth = (error) => {
        this.error = 'Identifiant ou mot de passe incorrect';
        this.loading = false;
    };
}

LoginController.$inject = ['$rootScope', '$location', 'AuthService']

export default LoginController;

如何解决我的错误?

1 个答案:

答案 0 :(得分:0)

类方法的语法是methodName(parameters) { body }

像这样写你的课:

class LoginController {

    constructor($rootScope, $location, AuthService) {
        this.AuthService = AuthService;
        this.$rootScope = $rootScope;
        this.$location = $location;
    }

    login() {
        this.loading = true;
        this.AuthService.login({
            username: this.username,
            password: this.password,
            rememberMe: this.rememberMe
        }).then(this.successAuth, this.failedAuth);
    }

    successAuth(result) {
        this.$rootScope.$broadcast('authenticationSuccess');
        this.$location.path('/');
    }

    failedAuth(error) {
        this.error = 'Identifiant ou mot de passe incorrect';
        this.loading = false;
    }

}