简体   繁体   English

在 Angular 中的自定义装饰器中访问服务

[英]Accessing a Service inside a custom decorator in Angular

I am creating a custom decorator in angular/typescript: @feature(featureName) which takes an input as string for the featureName and makes a call to service to check if the feature is enabled or not.我正在 angular/typescript 中创建一个自定义装饰器:@feature(featureName),它将输入作为 featureName 的字符串并调用服务以检查该功能是否启用。

I am unable to inject the service inside the decorator and have used various methods to do so.我无法在装饰器中注入服务,并且使用了各种方法来做到这一点。 Any possible solution?任何可能的解决方案? feature.decorator.ts feature.decorator.ts

// eslint-disable-next-line @typescript-eslint/ban-types
export function feature(flag: string) {
    return function (target: Object, key: string | symbol) {
        Object.defineProperty(target, key, {
            configurable: false,
            enumerable: false,
            get: () => {
                return 'newValue';
            },
            set: () => {
                return 'new value';
            }
        })
    }
}

My services is singleton service:我的服务是 singleton 服务:

import { BehaviorSubject, Observable } from 'rxjs';

import { BaseAPIService } from '../../services/base-api.service';
import { FEATURE } from './features-list.constants';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';

export interface FeatureStatus {
  feature: string;
  status: boolean;
}
@Injectable({ providedIn: 'root' })
export class FeatureManagerService {
  private _featureStatuses: FeatureStatus[];
  areFlagsAvailable$: BehaviorSubject<boolean> = new BehaviorSubject(false);
  constructor(private httpService: BaseAPIService) {}

  loadFeatureFlags(): Observable<boolean> {
    return this.fetchFeatureStatus().pipe(
      map((featureStatuses: FeatureStatus[]) => {
        console.log("FMS first");
        this._featureStatuses = featureStatuses;
        features = featureStatuses;
        this.areFlagsAvailable$.next(true);
        return true;
      })
    );
  }

  fetchFeatureStatus(): Observable<FeatureStatus[]> {
    const features = [];
    for (const feature of Object.keys(FEATURE)) {
      features.push(feature);
    }
    return this.httpService.post(`/api/feature-flag`, features);
  }

  isFeatureEnabled(featureName: string): boolean {
    return this._featureStatuses.filter((featureStatus) => {
      return featureStatus.feature === featureName;
    })[0].status;
  }
}

your code is a bit vague.你的代码有点含糊。 From what i understand is you get error when putting your service in constructor so here is my solution据我所知,将服务放入构造函数时会出错,所以这是我的解决方案

@Inject(BaseAPIService private httpService: any

hopefully this will help希望这会有所帮助

This article might be helpful for what you're trying to achieve本文可能对您要实现的目标有所帮助

https://blog.bitsrc.io/dependency-injection-using-decorators-77dd4c89968e https://blog.bitsrc.io/dependency-injection-using-decorators-77dd4c89968e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM