Babel未能编译es6角度工厂

时间:2017-05-09 23:54:23

标签: angular ecmascript-6 babel transpiler

我在es6上很新,但我现在已经做了一段时间的角度。

我不明白为什么下面的代码不会被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));




1 个答案:

答案 0 :(得分:1)

您的代码中存在语法错误... - 不允许在类体中使用const声明(在当前语法中,您只能在其中定义方法)。

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