Typescript:注入泛型并获取 ES6 模块名称
Typescript: Inject generic & get ES6 module name
我正在尝试使用以下方法构建一个通用存储库:
- 打字稿
- ES6
- 角度 1.x
但是我不知道应该如何注入实体然后获取其模块名称。
我想取名的原因:是因为我遵循一种命名约定,其中一个名为 order-count.ts 的文件应该呈现 URL '/order/count'
这可以用打字稿/Javascript解决吗?
这是我所拥有的:
订单模块.ts
import {App} from '../../App';
import {OrderService} from './order-service';
const module: ng.IModule = App.module('app.order', []);
module.service('orderService', OrderService);
订单服务网
import {CrudService} from '../../shared/services/crud-service'
import {OrderCount} from '../order/entities/order-count';
export class OrderService {
// @ngInject
constructor(private crudService: CrudService<OrderCount>) {
this.crudService = crudService;
}
getOrders() {
var promise = this.crudService.getAll();
promise.then(response => {
console.log(response, 'success');
}, error => {
console.log(error, 'failed');
});
}
}
订单计数.ts
import {Entity} from '../../../shared/models/entity';
export class OrderCount extends Entity {
storeId: string;
storeName: string;
}
实体.ts
export interface IEntity {
id: number;
}
实体.ts
import {IEntity} from '../../module/contracts/entities/entity';
export class Entity implements IEntity {
new() { }
id: number;
}
crud-service.ts
'use strict';
import { Entity } from '../models/entity';
import { EndpointService } from './endpointService';
export class CrudService<TEntity extends Entity> {
private baseCallPath: string;
private entity: { new (): Entity };
// @ngInject
constructor(private endpointService: EndpointService, private $http: ng.IHttpService) {
this.baseCallPath = new this.entity().constructor.name.replace('-', '/');
}
getAll(): ng.IHttpPromise<any> {
return this.handleResponse(
this.$http.get(this.endpointService.getUrl(this.baseCallPath)),
'getAll'
);
}
handleResponse(promise: ng.IHttpPromise<any>, callerMethodName: string): ng.IHttpPromise<any> {
return promise.success((data: any) => {
Array.prototype.push.apply(this.baseCallPath, data);
}).error((reason: any) => {
console.log(this.baseCallPath + callerMethodName, 'ERROR', reason);
});
}
}
endpoint-service.ts
export class EndpointService {
private baseUri: string = 'http://localhost:3000/api/';
getUrl(moduleName: string): string {
return this.baseUri + moduleName;
}
}
此链接可能有助于使用 Typescript 实现通用存储库
关于类名作为值的使用,您可以检查此相关问题。
好在它可以检索并用作Foo.name
或this.constructor.name
。不好的是,它并非在每个浏览器中都可用,应该进行多填充。另一个坏事是缩小的函数不会保存其原始名称。
Foo.name = 'Foo'
来注释功能的定义并坚持使用预制属性不是很好吗?没有。 Function.name
最初是不可配置的,因此它在大量浏览器中是只读的。
如果您根本不打算避免最小化,或者您不太喜欢配置 minifier 来保留类名(设计错误的解决方案(,请不要将 Function.name
用于类似的事情。
Angular 中可扩展 ES6/TS 类的典型情况是
export class Foo {
static _name = 'Foo';
}
export default angular.module('app.foo', [])
.factory('Foo', Foo)
// if DRY is a must,
// .factory(Foo._name, Foo)
.name;
import { Foo } from './foo';
export class Bar extends Foo {
static _name = 'Bar';
}
export default angular.module('app.bar', []).factory('Bar', Bar).name;
import moduleFoo from './foo';
import moduleBar from './bar';
angular.module('app', [moduleFoo, moduleBar]);
因此,Angular 模块和类的导出应该齐头并进,它们不可互换。
相关文章:
- 在一个表达式中加载并使用ES6模块
- ES6模块进口是否吊装
- 使用ES6模块创建JS
- 无法使用Webpack加载ES6模块
- 可以'I don’我不明白;使用webpack和es6模块进行EventEmitter
- 观察ES6模块属性
- NPM:取决于本地 ES6 模块(无转译)
- 使用 Babel 转译器导入类并调用带有 ES6 模块的静态方法
- 使用 Babel 将 ES6 模块转换为 ES5 AMD 模块,无法按预期工作
- ES6模块导入和依赖关系管理
- browserify/requirejs模块和ES6模块有什么区别
- 使用es6模块:未捕获ReferenceError:define未定义
- 如何通过名称(从模块本身)访问ES6模块变量
- 如何有条件地导入 ES6 模块
- 使用webpack和babel加载程序导入导出ES6模块
- ES6模块初始化顺序错误
- ES6模块:为什么先前导出的模块不为“;“孩子”;模块
- 流星 ES6 模块相对路径
- ES6 模块不适用于 babel 6 和 gulp
- 使用 webpack 和 ts-loader 重新导出 ES6 模块