【发布时间】:2019-01-24 16:22:32
【问题描述】:
我有一个 AngularJS + TypeScript 应用程序,但我不断收到以下错误:
错误:$resource:badcfg 响应与配置的参数不匹配
动作的资源配置错误:预期响应包含一个get但得到一个对象(请求:数组GET)
我的 AngularJS 版本是 1.4.8,TypeScript 版本是 3.0.1。我的 tsconfig.json 看起来像这样:
{
"compileOnSave": true,
"compilerOptions": {
"allowJs": true,
"lib": [ "dom", "es5", "scripthost" ],
"noImplicitAny": true,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"include": [
"App/TypeScriptCode/**/*.ts",
"typings"
],
"exclude": [
"node_modules",
"definitions",
"wwwroot",
"App/Old",
"Scripts",
"*.js"
]
}
这是我的 package.json
{
"dependencies": {
"@types/angular": "^1.6.49",
"@types/angular-resource": "^1.5.14",
"@types/uuid": "^3.4.3",
"npm": "^6.3.0",
"ts-node": "^7.0.0"
},
"devDependencies": {
"angular": "1.5.0",
"bootstrap": "3.3.6",
"grunt": "0.4.5",
"grunt-bower-task": "0.4.0",
"typescript": "^3.0.1",
"typings": "1.3.0"
},
"name": "my-app",
"private": true,
"scripts": {
"demo": "./node_modules/.bin/ts-node ./test.ts"
},
"version": "1.0.0"
}
错误发生在以下控制器中的函数 GetEmployeeTypes 中:
namespace App {
'use strict';
class EmployeeTypeController {
noRecords: boolean;
employeeTypeApi: IEmployeeTypeApi;
storage: ngStorage.IStorageService;
///
public constructor($localStorage: ngStorage.IStorageService, employeeTypeApi: IEmployeeTypeApi) {
this.noRecords = true;
this.employeeTypeApi = employeeTypeApi;
this.storage = $localStorage;
this.GetEmployeeTypes();
}
///
public GetEmployeeTypes(): void {
const employeeTypes: IEmployeeType[] = this.employeeTypeApi.employeeType.get(function success(): void {
this.storage.employeeTypes = employeeTypes;
});
};
}
angular
.module('App')
.controller('EmployeeTypeController', EmployeeTypeController);
}
用于从后端加载员工类型的 api 服务 EmployeeTypeApi 如下所示:
namespace App {
'use strict';
export interface IEmployeeTypeApi {
employeeType: IEmployeeTypeClass;
}
export class EmployeeTypeApi implements IEmployeeTypeApi {
constructor(public $resource: angular.resource.IResourceService) {}
publishDescriptor: angular.resource.IActionDescriptor = {
method: 'GET',
isArray: true
};
public employeeType: IEmployeeTypeClass = this.$resource<IEmployeeType[], IEmployeeTypeClass>('https://localhost:44300/api/employeetypes', null, {
publish: this.publishDescriptor
});
}
// Now register the service with the Angular app
angular
.module('App')
.service('employeeTypeApi', EmployeeTypeApi);
}
最后,这里是 IEmployeeType 和 IEmployeeTypeClass:
namespace App {
export interface IEmployeeType extends angular.resource.IResource<IEmployeeType> {
id: number;
clientId: number;
employeeTypeName: string;
description: string;
$publish(): IEmployeeType;
$unpublish(): IEmployeeType;
}
}
namespace App {
export interface IEmployeeTypeClass extends angular.resource.IResourceClass<IEmployeeType[]> {
get(): IEmployeeType[] ;
get(onSuccess: Function): IEmployeeType[];
publish(): IEmployeeType[];
}
}
所以我在这里想要实现的是从后端加载一组 IEmployeeType 对象。后端本身是一个 ASP.NET Web API 2 服务,由许多控制器组成。我已经验证正确调用了加载 Employee Type 数据的控制器,并且它确实返回了一个 EmployeeType 实例数组,这意味着问题很可能出在前端代码的某个地方。
正如您在 EmployeeTypeApi 中看到的,我已明确将 isArray 标志设置为“true”,因此我不确定为什么会发生错误。我在这里做错了什么?
【问题讨论】:
标签: angularjs typescript asp.net-web-api angular-resource