解析器不起作用-角度

时间:2018-12-03 17:16:21

标签: angular routing angular5 resolve

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 { }

我正在尝试在我的项目中使用解析器。上面显示的是配置。我在控制台上遇到错误,浏览器中无任何显示..在添加解析器之前它正在工作..我认为解析器配置存在问题 这是我面临的错误。感谢任何帮助

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

1 个答案:

答案 0 :(得分:1)

stateOptions数组中删除imports。它是服务而不是模块。应该仅导入模块。

imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    AppRoutingModule,
   //StateOptions
  ],