【问题标题】:angular 2 create custom variables to perfomm CRUD on specified urlangular 2 创建自定义变量以在指定的 url 上执行 CRUD
【发布时间】:2016-08-24 09:27:00
【问题描述】:

下面是一个包含一些http请求的模型。我的问题是我可以通过实例化一个新的类对象来实例化服务吗?

export class DbHttp {

    private path:string;

    constructor(private path:string, private customService:CustomService){}

    create(data:Object):Observable<Response>{
        return this.customService.create(this.path, data)
    }

    getAll():Observable<Response>{
        return this.customService.getAll(this.path)
    }
}

我想要实现的是在自定义 url 上执行 http 请求的快捷方式。理想情况下,语法应该是这样的

public dbAccessTodo = this.service.access('todo')
//access is a method which returns an object with builtin CRUD functions for a Base_Url+'todo'

create(data){
  this.dbAccessTodo.create(data)
}

谢谢!

【问题讨论】:

    标签: angular crud angular2-services angular-http


    【解决方案1】:

    我不确定你想要实现什么。

    我创建了这样的服务:

    @Injectable()
    export class OrderService {
        private endPoint = 'Orders';
    
        constructor(private http: Http) { }
    
        get(id: string) {
            return this.http
                .get(`'${this.endPoint}/${id}`, { body: '' })
                .map(res => res.json());
        }
    

    要更改我的 api 的基本 URL,我使用 CustomRequestOptions

    export class CustomRequestOptions extends RequestOptions {
        merge(options?: RequestOptionsArgs): RequestOptions {
            ...
            options.url = '<%= API_BASE_URL %>' + options.url;
            var result = super.merge(options);
            result.merge = this.merge;
            return result;
    }
    

    希望这会有所帮助,否则,请更详细地解释您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2016-11-24
      相关资源
      最近更新 更多