ESlint 在编译 es6 导入/导出语句时失败

Posted

技术标签:

【中文标题】ESlint 在编译 es6 导入/导出语句时失败【英文标题】:ESlint fails on compiling es6 import/export statements 【发布时间】:2016-11-20 15:12:26 【问题描述】:

我正在尝试使用类将我现有的 Angular 应用程序转换为 es6。但是 eslint 在遇到导入/导出语句时无法编译我的 js 文件。

class HeaderController 

    constructor($rootScope, $scope) 
        this.$rootScope = $rootScope;
        this.$scope = $scope;
    

    changeNavItem(selectedTab) 
        this.activeNavItem = selectedTab;
    

    init() 
        this.activeNavItem = 'All';
     

HeaderController.$inject = ['$rootScope', '$scope'];

angular.module('app')
    .controller('HeaderController', HeaderController);

export default HeaderController; // fails on this line

ESlint 抛出此错误'由于解析错误无法处理源代码 'import' 和 'export' 可能只出现在 'sourceType: module''

我已经为 ESlint 添加了必要的配置,但仍然失败。

我在我的 eslintrc.json 中添加了以下内容

"parserOptions": 
        "ecmaVersion": 6,
        "sourceType": "module",
        "allowImportExportEverywhere": true,
        "ecmaFeatures": 
            "modules": true,
            "arrowFunctions": true,
            "classes": true
        
    

请帮助我如何解决这个问题,还有一个使用 gulp 以角度设置 es6 的完整示例将非常有帮助。

【问题讨论】:

【参考方案1】:

请尝试将您的 configuration 文件从 eslintrc.json 重命名为 .eslintrc.json

【讨论】:

是的,我的文件命名正确,即 .eslintrc.json

以上是关于ESlint 在编译 es6 导入/导出语句时失败的主要内容,如果未能解决你的问题,请参考以下文章