Javascript Typescript:injectgeneric&;获取ES6模块名称

Javascript Typescript:injectgeneric&;获取ES6模块名称,javascript,angularjs,generics,typescript,ecmascript-6,Javascript,Angularjs,Generics,Typescript,Ecmascript 6,我正在尝试使用以下内容构建通用存储库: 打字稿 ES6 角度1.x 但我不知道应该如何注入实体,然后获取其模块名 我想得到这个名字的原因是: 这是因为我遵循命名约定,名为order-count.ts的文件应该呈现URL“/order/count” 这可以用Typescript/Javascript解决吗 以下是我所拥有的: order-module.ts 订购服务 实体.ts 实体.ts crud-service.ts 将类名的用法视为您可以检查的值 好消息是它可以被检索并用作Foo.nam

我正在尝试使用以下内容构建通用存储库:

  • 打字稿
  • ES6
  • 角度1.x
但我不知道应该如何注入实体,然后获取其模块名

我想得到这个名字的原因是: 这是因为我遵循命名约定,名为order-count.ts的文件应该呈现URL“/order/count”

这可以用Typescript/Javascript解决吗

以下是我所拥有的:

order-module.ts

订购服务

实体.ts

实体.ts

crud-service.ts


将类名的用法视为您可以检查的值

好消息是它可以被检索并用作
Foo.name
this.constructor.name
。糟糕的是,它不是在每个浏览器中都可用,应该是多填充的。另一个缺点是,缩小后的函数将无法保存其原始名称

在函数的定义上使用
Foo.name='Foo'
注释函数,并坚持使用预先生成的属性,这不是很好吗?不是真的
Function.name
是只读的,因此在很多浏览器中都是只读的

如果您根本不打算避免最小化,或者您不太喜欢配置minifier来保留类名(这是一种设计上有缺陷的解决方案),那么不要使用
Function.name
来实现类似的功能

可扩展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;



因此,Angular模块和类的导出应该齐头并进,它们是不可互换的。

可能有助于实现带有Typescript的通用存储库。

实体未定义为Angular服务,这意味着它不能通过Angular DI注入。不清楚“ES6模块名称”可能指的是什么,但如果您想在应用程序中找出“../../../../shared/models/entity”字符串,则不可能。@estus我知道我没有将该实体定义为角度服务。问题是我将有多个扩展实体类的类。那么这个问题可以不用角度注入来解决吗?关于ES6模块名称,这就是我的意思,但正如你所说,我认为这可能是不可能的。我有没有可能改用类名?将能够将其解析为URL。
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');
        });
    }
}
import {Entity} from '../../../shared/models/entity';

export class OrderCount extends Entity {
    storeId: string;
    storeName: string;
}
export interface IEntity {
    id: number;
}
import {IEntity} from '../../module/contracts/entities/entity';

export class Entity implements IEntity {
    new() { }
    id: number;
}
'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);
        });
    }
}
export class EndpointService {
    private baseUri: string = 'http://localhost:3000/api/';

    getUrl(moduleName: string): string {
        return this.baseUri + moduleName;
    }
}
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]);