angular - Angular @Optional 指令注入被忽略
问题描述
我收到这样的错误
Can't resolve all parameters for ControlErrorsDirective: (?, [object Object], [object Object], [object Object], [object Object], ?).
有服务声明。
constructor(
@Optional() @Host() private form: FormSubmitDirective,
private control: NgControl,
private resolver: ComponentFactoryResolver,
public viewContainerRef: ViewContainerRef,
@Inject(FORM_ERRORS) private errors,
@Optional() controlErrorContainer: ControlErrorContainerDirective
) {
我看到注入问题与可选指令服务有关。是的,指令没有设置到组件中。但它们是可选的。为什么会调用错误?是的,我向模块提供了所有指令。
@NgModule({
declarations: [
AppComponent,
ControlErrorsDirective,
ControlErrorContainerDirective,
FormSubmitDirective,
ControlErrorComponent
],
entryComponents: [
ControlErrorComponent
],
但它不起作用。这个错误似乎微不足道。但我没有看到它。在这里效果很好。https://stackblitz.com/edit/angular-terms-and-conditions-reactive-forms?embed=1&file=src/app/form-errors.ts 问候。
解决方案
诸如此类的错误:
无法解析Something的所有参数:(?,
通常意味着在执行装饰器时,构造函数中用于参数的类型是未定义的。
它可能发生的原因有几个:
您在 DI 中使用的类之间存在循环依赖关系
您将所有类都放在一个文件中,并在声明之前尝试使用类型。所以顺序很重要。
在您的情况下,您将所有类都写在一个文件中,但尝试使用FormSubmitDirective
and ControlErrorContainerDirective
before ControlErrorsDirective
class 这是您问题的主要原因。
推荐阅读
- c - 在 Nvidia Jetson 上除以零不会导致运行时异常
- r - 是否有一个 R 函数可以从具有许多 NA 值的逗号分隔字符串中仅提取数字以创建仅包含数字的列?
- java - 如何使用泛型将类及其类型作为参数传递?
- php - 使用 eloquont 从表中删除记录
- angular - HttpParams 不请求过滤的 JSON
- c++ - 使用“-ffast-math”在 GCC 上与 quiet_NaN 进行浮点比较总是产生真(!)
- c++ - 如何使用 std::regex 获取所有捕获?
- jmeter - JMeter 当控制器命中 HTTP POST 请求 5 次时,每个 POST 需要 5 个不同的变量
- r - 如何将行值重新格式化为 R 中的列字段
- forms - 如何将表单中的列表选择显示到 PDF