簡體   English   中英

Babel無法編譯es6 angular工廠

[英]Babel failing to compile es6 angular factory

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

您的代碼中有語法錯誤…-不允許在類主體中使用const聲明(在當前語法中,您只能在其中定義方法)。

相反,您需要將此const聲明為對象屬性,或將其從類聲明中移出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM