4

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

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

我错过了什么?

4

2 回答 2

1

看看这条线let component = new NotificationsComponent();

当你创建一个新对象时,你没有提供参数,NotificationsComponent而它的构造函数需要一个 type 的对象ElementRef

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

于 2016-07-26T00:36:34.283 回答
0

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

于 2016-07-27T03:52:10.733 回答