如何从 Angular 包中排除服务

问题描述 投票:0回答:0

想象一下我在环境中有一个设置,对于不同的环境可能是真或假。

我有一个抽象服务和两个实现:

@Injectable()
export abstract class TestCommonService {
  protected abstract uniqueId: string;
}
export class TestAService {  
  protected uniqueId = 'test_a';
}
export class TestBService {  
  protected uniqueId = 'test_b';
}

我在一个模块中提供这个:


{ provide: TestCommonService,
  useFactory: () => environment.feature ? new TestAService() : new TestBService()
}

有没有办法从捆绑中排除不必要的服务?

因为我们在构建时知道,如果 env 属性是真还是假,我相信,我们可以做一些事情。

我尝试对

@Injectable({providedIn: 'root', useFactory: ...})
做同样的事情,但它也不排除捆绑包中不必要的服务。

angular dependency-injection bundle tree-shaking
© www.soinside.com 2019 - 2024. All rights reserved.