Angular 2 Release"意外的价值' ElementRef'由模块导入"

时间:2016-10-01 15:19:46

标签: javascript angular

我升级到Angular 2的版本,我正在尝试使用ElementRef。首先,我收到了此处指定的错误projectile-invalidate-cacheAngular2 RC5 error:zone.js: Unhandled Promise rejection: No provider for ElementRef所以我将代码更改为:

Angular2 RC5 error:zone.js: Unhandled Promise rejection: No provider for ElementRef

现在我收到错误import { NgModule, CUSTOM_ELEMENTS_SCHEMA, ElementRef } from '@angular/core'; @NgModule({ declarations: [DashboardComponent, WidgetBankComponent, DataTableDirectives, OrderBy], exports: [DashboardComponent, WidgetBankComponent], imports: [BrowserModule, HttpModule, FormsModule, ChartsModule, ElementRef], providers: [ChartService, GridService, WidgetsControlService, GridViewService, ApplicationSettingsService, DataService, ToolsService, LocalStorageService, RuntimeCompiler, COMPILER_PROVIDERS, NgGrid, NgGridItem], schemas: [CUSTOM_ELEMENTS_SCHEMA] })

任何想法?

提前谢谢!

编辑

下面的答案中是否有建议的修复,现在有这个错误 - 这是完整的错误 - 有没有办法告诉我需要从这个错误中提供提供商?

(SystemJS) Unexpected value 'ElementRef' imported by the module 'DashboardModule'

2 个答案:

答案 0 :(得分:1)

尝试从ElementRef数组中移除imports并将其粘贴到providers数组中:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA, ElementRef } from '@angular/core';
@NgModule({
    declarations: [DashboardComponent, WidgetBankComponent, DataTableDirectives, OrderBy],
    exports: [DashboardComponent, WidgetBankComponent],
    imports: [BrowserModule, HttpModule, FormsModule, ChartsModule],
    providers: [ChartService, GridService, WidgetsControlService, GridViewService, ApplicationSettingsService, DataService, ToolsService, LocalStorageService, RuntimeCompiler, COMPILER_PROVIDERS, NgGrid, NgGridItem, ElementRef],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

答案 1 :(得分:0)

在您需要的个别组件级别处使用它。

import {  ElementRef } from '@angular/core';
@Component({
 ....
})
export class DashboardComponent{
    constructor(private el:ElementRef){}
}