2016-10-01 68 views
1

我升級到了Angular 2的發佈版本,我試圖使用ElementRef。起初,我得到了錯誤Angular2 RC5 error:zone.js: Unhandled Promise rejection: No provider for ElementRef爲指定的位置:Angular2 RC5 error:zone.js: Unhandled Promise rejection: No provider for ElementRef所以我改變了我的代碼:Angular 2發佈「意外值'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'

什麼想法?

預先感謝您!

編輯

難道在下面的答案建議的修復,現在有這樣的錯誤 - 這裏是完整的錯誤 - 有沒有辦法知道,我需要從這個錯誤中供應商?

zone.js:355 Unhandled Promise rejection: No provider for ElementRef! ; Zone: <root> ; Task: Promise.then ; Value: NoProviderError {_nativeError: Error: No provider for ElementRef! 
    at NoProviderError.Error (native) 
    at NoProviderError.Base…, keys: Array[1], injectors: Array[1]}_nativeError: Error: No provider for ElementRef! 
    at NoProviderError.Error (native) 
    at NoProviderError.BaseError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1248:38) 
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1703:20) 
    at new NoProviderError (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1734:20) 
    at ReflectiveInjector_._throwOrNull (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3331:23) 
    at ReflectiveInjector_._getByKeyDefault (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3359:29) 
    at ReflectiveInjector_._getByKey (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3322:29) 
    at ReflectiveInjector_.get (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3131:25) 
    at NgModuleInjector.AppModuleInjector.createInternal (AppModule.ngfactory.js:310:75) 
    at NgModuleInjector.create (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:7192:80)constructResolvingMessage: (keys)injectors: Array[1]keys: Array[1]message: (...)name: (...)stack: (...)__proto__: AbstractProviderError Error: No provider for ElementRef! 
    at NoProviderError.Error (native) 
    at NoProviderError.BaseError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1248:38) 
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1703:20) 
    at new NoProviderError (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1734:20) 
    at ReflectiveInjector_._throwOrNull (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3331:23) 
    at ReflectiveInjector_._getByKeyDefault (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3359:29) 
    at ReflectiveInjector_._getByKey (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3322:29) 
    at ReflectiveInjector_.get (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3131:25) 
    at NgModuleInjector.AppModuleInjector.createInternal (AppModule.ngfactory.js:310:75) 
    at NgModuleInjector.create (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:7192:80)consoleError @ zone.js:355_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 
zone.js:357 Error: Uncaught (in promise): Error: No provider for ElementRef!(…)consoleError @ zone.js:357_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 
+0

這angular2的版本,您使用的? – micronyks

+0

最終版本 – blubberbo

+0

其奇怪!它應該工作。最終版本中使用哪個zone.js版本? – micronyks

回答

0

使用它在個別組件級別在你需要它。

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

然後我得到'zone.js:355未處理的Promise拒絕:沒有ElementRef的提供者! ;區域:;任務:Promise.then;值:NoProviderError' ...這導致我相信我在某處引用它,但我不知道如何找出位置。它可能是我擁有的一個插件,所以我想嘗試添加它作爲所有插件的提供者 – blubberbo

+0

從'imports'中移除'ElementRef'並按照我所說的去做。 – micronyks

+0

我從導入中刪除了它,並且像你說的那樣將它添加到了組件中,現在我得到了'沒有提供程序...' – blubberbo

1

嘗試從imports陣列刪除ElementRef並將其粘貼到的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] 
}) 
+0

創建了與原始發佈相同的錯誤:( – blubberbo

+0

@blubberbo,您從根項目文件夾運行'tsc'? – ktretyak

+0

這是正確的 – blubberbo

相關問題