简体   繁体   中英

Uncaught (in promise): No provider for Idle

I am working with ng2-idle in a AngularJS 2 application. I was able to get the ng2-idle package in the node_modules folder of the project. I am trying to import it into one of my components like the following:

Dashboard.component.ts:

import { Ng2IdleModule } from 'ng2-idle/index';
import { DEFAULT_INTERRUPTSOURCES } from 'ng2-idle/index';
import { Idle } from 'ng2-idle/idle';

//location and router
import { Router } from '@angular/router';


import { AppComponent } from '../../app.component';

@Component({

    selector: 'dashboard',
    templateUrl: './dashboard.component.html'
})

export class DashboardComponent {
    private showBusyIndicator: boolean = false;
    private idleState: string = 'Not started.';
    private timedOut: boolean = false;
    constructor( @Host() @Inject(forwardRef(() => AppComponent)) private app: AppComponent,
        private idle: Idle) {//throws an error when I include it in the constructor

    }

}

When I do that I get the following error:

message:Uncaught (in promise): Error: Error in ./DashboardComponent class DashboardComponent_Host - inline template:0:0 caused by: No provider for Idle!
Error: No provider for Idle!
    at NoProviderError.BaseError [as constructor] (http://localhost:4200/main.bundle.js:7103:34)
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:4200/main.bundle.js:62865:16)
    at new NoProviderError (http://localhost:4200/main.bundle.js:62896:16)
    at ReflectiveInjector_._throwOrNull (http://localhost:4200/main.bundle.js:101281:19)
    at ReflectiveInjector_._getByKeyDefault (http://localhost:4200/main.bundle.js:101309:25)
    at ReflectiveInjector_._getByKey (http://localhost:4200/main.bundle.js:101272:25)
    at ReflectiveInjector_.get (http://localhost:4200/main.bundle.js:101081:21)
    at AppModuleInjector.NgModuleInjector.get (http://localhost:4200/main.bundle.js:63774:52)
    at ElementInjector.get (http://localhost:4200/main.bundle.js:101472:48)
    at ElementInjector.get (http://localhost:4200/main.bundle.js:101472:48)
source: 
 stack trace:null

When I import it in the AppModule I get the following error:

metadata_resolver.js:227Uncaught Error: Unexpected value 'Ng2IdleModule' imported by the module 'AppModule'(…)

I did not import the module in my app.module.ts. Is that why I am getting this error? Could you let me know how I could resolve this issue.

According to the ng2 idle example app, you need to import the module with .forRoot() .

https://github.com/HackedByChinese/ng2-idle-example#set-up-your-application-module

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MomentModule,
    NgIdleKeepaliveModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Use import {NgIdleModule} from '@ng-idle/core' instead of NgIdleKeepaliveModule. Reference : https://github.com/HackedByChinese/ng2-idle-example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM