首页 > 解决方案 > ts 生成一个带有错误的 js(预期在 'case' 之前有一个 'break' 语句

问题描述

我有这个调用 api 并返回结果的打字稿代码

export default class ClientsService{

async GetClients() : Promise<IClientsModel[]>
{
    let responseResult : IClientsModel[] = [] ;
    const response = await fetch("https://localhost:5001/api/clients");
    if (response.ok) { 

        const tmp  =  await response.json();
        responseResult =   JSON.parse(tmp) as ClientsModel[];  
    } 
   return responseResult ; 
}

}

这会生成一个带有此错误的js:在“case”之前需要一个“break”语句。(案例 3 之前的行)

    import { __awaiter, __generator } from "tslib";
var ClientsService = /** @class */ (function () {
    function ClientsService() {
    }
    ClientsService.prototype.GetClients = function () {
        return __awaiter(this, void 0, void 0, function () {
            var responseResult, response, tmp;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        responseResult = [];
                        return [4 /*yield*/, fetch("https://localhost:5001/api/clients")];
                    case 1:
                        response = _a.sent();
                        if (!response.ok) return [3 /*break*/, 3];
                        return [4 /*yield*/, response.json()];
                    case 2:
                        tmp = _a.sent();
                        responseResult = JSON.parse(tmp);
                        _a.label = 3;
                    case 3: return [2 /*return*/, responseResult]; // Error here 
                }
            });
        });
    };
    return ClientsService;
}());
export default ClientsService;
//# sourceMappingURL=ClientsService.js.map

标签: typescript

解决方案


推荐阅读