【问题标题】:Angular 2 Supplied parameters do not match any signature of call target (spec.ts)Angular 2 提供的参数与调用目标(spec.ts)的任何签名都不匹配
【发布时间】:2016-07-26 00:01:25
【问题描述】:

我正在使用 Angular2 Cli 构建 Angular2 应用程序。 我有一个notifications.component,它有一个像这样的构造函数

constructor(private _elRef: ElementRef) {}

当我构建 (npm start) 时出现此错误

...angular2/tmp/broccoli_type_script_compiler-input_base_path-wKrIZXNv.tmp/0/src/app/notifications/notifications.component.spec.ts (10, 21): Supplied parameters do not match any signature of call target

angular cli生成的文件notifications.component.spec.ts是这样的

import { By }           from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { addProviders, async, inject } from '@angular/core/testing';
import { NotificationsComponent } from './notifications.component';

describe('Component: Notifications', () => {
 it('should create an instance', () => {
 let component = new NotificationsComponent();
 expect(component).toBeTruthy();
 });
});

但是,如果我在没有构造函数参数的情况下构建一切正常。如果我在构建后添加此参数,则一切正常。

我错过了什么?

【问题讨论】:

    标签: angular angular2-components


    【解决方案1】:

    看看这行let component = new NotificationsComponent();

    当您为NotificationsComponent 创建一个新对象时,您没有提供参数,而它的构造函数需要一个ElementRef 类型的对象。

    这就是为什么你在没有构造函数参数的情况下构建一切正常。

    【讨论】:

    • 我注意到了,但我试图提供一个论点,但任何一个都不起作用!你有什么建议?
    • 如果您的NotificationsComponent 不需要ElementRef,只需将其从构造函数中删除即可。否则,您可能需要检查您正在使用哪个测试框架并向其提供存根。
    • 我可以删除,但我想知道如何修复它!
    【解决方案2】:

    嗨,兄弟,有一个临时解决方案,因为我遇到了同样的问题,没有人无法解释这是如何发生的以及发生了什么,但您可以尝试进入 notification.component.ts 并将构造函数更改为:“构造函数(私有_elRef:any = ElementRef){}” 重新加载然后将其更改回原来的样子

    【讨论】:

    • 嘿伙计,感谢分享这个想法!但是,它仍然出现错误。 zone.js:463 错误:未捕获(承诺中):错误:错误:无法解析 NotificationsComponent 的所有参数:(?)。(...)。 Notifications.components.spec.ts 中的所有代码我暂时都注释掉了!
    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多