0

state.service.ts

import { Injectable } from "@angular/core";
import {Http} from "@angular/http";
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router";
import { Observable } from "rxjs/Observable";
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/delay';
@Injectable()
export class StateOptions implements Resolve<Observable<string>> {

   constructor(private http:Http) {
}

resolve() {
    return Observable.of('Hello Alligator!').delay(2000);
  }
}

routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import {StateOptions} from './state.service';

const routes: Routes = [
  {
    path: '',
    component:AppComponent,
    resolve: { message: StateOptions }    
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [StateOptions]
})
export class AppRoutingModule {
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
.....
import { StateOptions } from './state.service'



@NgModule({
  declarations: [
    HomeComponent,
    AppComponent,  
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    AppRoutingModule,
    StateOptions


  ],
  exports: [

  ],
  providers: [StateOptions],
  bootstrap: [HomeComponent]
})
export class AppModule { }

I am trying to use resolver in my project . The above shown is the configuration. I am getting error on console, nothing shows in browser..it was working before adding resolver..i think problem is with the resolver configuration this is the error i am facing..appreciate any help

 Unexpected value 'StateOptions' imported by the module 'AppModule'. Please add a @NgModule annotation.

1 Answer 1

1

Remove stateOptions from imports array. Its a service and not a module. Only modules are supposed to be imported.

imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    AppRoutingModule,
   //StateOptions
  ],
Sign up to request clarification or add additional context in comments.

2 Comments

Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[StateOptions -> Http]: StaticInjectorError(Platform: core)[StateOptions -> Http]: NullInjectorError: No provider for Http! _NullInjector.prototype.get@webpack-internal:///./node_modules/@angular/core/esm5/core.js:1219:19 resolveToken@webpack-internal:///./node_modules/@angular/core/esm5/core.js:1517:17 tryResolveToken@webpack-internal:///./node_modules/@angular.....
If you google your error stackoverflow.com/questions/47492475/…. In your service you are using Http but you have imported HttpClientModule. Use HttpClient class instead of Http.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.