Angular 2 提供的参数与调用目标(spec.ts)的任何签名都不匹配

标签 angular angular2-components

我正在使用 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();
 });
});

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

我错过了什么?

最佳答案

看这行 let component = new NotificationsComponent();

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

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

关于Angular 2 提供的参数与调用目标(spec.ts)的任何签名都不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579151/

相关文章:

javascript - 如何从 Angular 2 中的另一个 Promise 函数调用一个 Promise 函数?

angular - 子组件中的 ExpressionChangedAfterItHasBeenCheckedError

javascript - 读取 Angular Reactive Form 中的复选框 FormControl 值

php - 如何在数据库事件发生时自动发送Web推送通知? Angular + PHP + MySQL

angular2-template - Angular 2 扩展组件 - 如何继承样式

angular - node_modules/ngx-toastr/toastr/toast-noanimation.component.d.ts(19,9)

angular - 如何在 Angular 2 中将方法从一个组件调用到另一个组件?

cordova - Ionic/Angular 2 组件事件发射器不更新 View

css - 由于@Font-Face 在 Chrome 中呈现问题

Angular 2,无法从另一个组件访问一个组件中的变量