小编Fil*_*ano的帖子

Angular:如何将字符串发送到注入服务?

我为 crud 任务创建了通用服务,该服务通过 DI(依赖注入)使用 HttpClient,但我需要在服务的构造函数中通知另一个值,如何实现?

因为当我在我的类的构造函数中定义它将使用 DI 使用 CRUD 服务时,无法将参数传递给构造函数

下面是服务

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { CrudInterface } from "@app/core/crud.interface";
import { environment } from "@env/environment";
import { Observable } from "rxjs/Observable";

@Injectable()
export class CRUD<T> implements CrudInterface<T>{
    
    endpoint: string;

    constructor(private http: HttpClient, routeDir: string){
        this.endpoint = `${environment.endpoint}/${routeDir}`;
    }
    
    getAll(): Observable<T[]> {
        return this.http.get<T[]>(`${this.endpoint}`);
    }

    get(id: number): Observable<T> {
        return this.http.get<T>(`${this.endpoint}/${id}`);
    }

    create(object: T): Observable<T> {
        return this.http.post<T>(`${this.endpoint}`, object);
    }

    update(object: …
Run Code Online (Sandbox Code Playgroud)

dependencies dependency-injection code-injection angular

6
推荐指数
1
解决办法
4816
查看次数