Babel 无法编译 es6 angular 工厂

Babel failing to compile es6 angular factory

我是 es6 的新手,但我已经 angular 有一段时间了。

我不明白为什么下面的代码不能被 Babel 转译。通过粘贴到 https://babeljs.io/ 中自行测试。

错误是它期望在 const singleDocumentSample 的开头有一个 (,这似乎是一个错误报告的错误。我尝试了

的变体

const xx; xx = {}; 以防它与初始化相关。我尝试更改为 let 甚至 var。怎么回事?

完整代码文件如下:

class FakeData {
    constructor($httpBackend) {
        'ngInject';
        this.$httpBackend = $httpBackend;
    }

    initialize() {
        this.$httpBackend.whenGET('/v1/api/documents').respond(function () {
            return [200, getAllDocuments()];
        });

        this.$httpBackend.whenGET(/\/v1\/api\/documents\/[1-9][0-9]*/).respond(function () {
            return [200, getDocumentById()];
        });
    }

    getAllDocuments() {
        return allDocumentsSample;
    }

    getDocumentById() {
        return singleDocumentSample;
    }

    const singleDocumentSample = {
        "id": "5728fdb8e4b04adb356afb87",
        "fileName": "6454841.xlsx",
        "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "fileExtension": "xlsx",
        "uploadDate": "2016-05-03T19:36:24Z",
        "thumbnails": [

        ],
        "thumbnailsDetailed": null,
        "fileSize": 11467,
        "description": "",
        "position": null,
        "confidential": null,
        "customMetadata": null,
        "who": {
            "creationDate": "2016-05-03T19:36:24Z",
            "lastUpdateDate": "2016-05-03T19:36:24Z",
            "createdBy": {
                "userName": "hawkslee",
                "fullName": "Hawksley, Emma",
                "userId": 49952
            },
            "lastUpdatedBy": {
                "userName": "hawkslee",
                "fullName": "Hawksley, Emma",
                "userId": 49952
            }
        },
        "url": "http://attachments-api.apps.wwt.com/api/attachments/5728fdb8e4b04adb356afb87/file"
    };

    const allDocumentsSample = {
        "data": [
            {
                "id": "5728fdb8e4b04adb356afb87",
                "fileName": "6454841.xlsx",
                "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "fileExtension": "xlsx",
                "uploadDate": "2016-05-03T19:36:24Z",
                "thumbnails": [

                ],
                "thumbnailsDetailed": null,
                "fileSize": 11467,
                "description": "",
                "position": null,
                "confidential": null,
                "customMetadata": null,
                "who": {
                    "creationDate": "2016-05-03T19:36:24Z",
                    "lastUpdateDate": "2016-05-03T19:36:24Z",
                    "createdBy": {
                        "userName": "hawkslee",
                        "fullName": "Hawksley, Emma",
                        "userId": 49952
                    },
                    "lastUpdatedBy": {
                        "userName": "hawkslee",
                        "fullName": "Hawksley, Emma",
                        "userId": 49952
                    }
                },
                "url": "http://attachments-api.apps.wwt.com/api/attachments/5728fdb8e4b04adb356afb87/file"
            }
        ],
        "pagination": {
            "page": 1,
            "pageSize": 50,
            "pageCount": 456,
            "sort": "id",
            "order": "asc",
            "itemCount": 22799,
            "nextPageUrl": null,
            "previousPageUrl": null
        }
    };
}

appModule.factory('fakeData', $httpBackend => new FakeData($httpBackend));

您的代码中存在语法错误... - 在 class 正文中不允许有 const 声明(在当前语法中,您只能在那里定义方法)。

相反,您需要将此常量声明为对象属性,或将其从 class 声明中移出。