0

将 Angular 4 和 REST 应用程序与服务连接时出现错误。

以下是错误:

compiler.es5.js:1694 Uncaught Error: Can't resolve all parameters for TypeaheadComponent: (?, [object Object], [object Object]).
    at syntaxError (compiler.es5.js:1694)
    at CompileMetadataResolver._getDependenciesMetadata (compiler.es5.js:15781)
    at CompileMetadataResolver._getTypeMetadata (compiler.es5.js:15649)
    at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.es5.js:15244)
    at CompileMetadataResolver._getEntryComponentMetadata (compiler.es5.js:15903)
    at eval (compiler.es5.js:15889)
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver._getEntryComponentsFromProvider (compiler.es5.js:15888)
    at eval (compiler.es5.js:15852)
    at Array.forEach (<anonymous>)

我已经检查并仔细检查了我的路由器设置是否有任何错误,但我自己似乎找不到任何错误。任何建议表示赞赏。

这是我的 app-routing.module.ts 代码:

import {Routes, RouterModule } from '@angular/router';
import {NgModule} from '@angular/core';
import {TypeaheadComponent} from "./shared/typeahead-component";
import {DashboardService} from "./services/dashboard-service";

const routes: Routes = [
    {  path: 'typeahead-component/', component: TypeaheadComponent }
]

@NgModule({
  imports: [RouterModule.forRoot(routes, { enableTracing: true } )],
    exports: [RouterModule],
    providers: [DashboardService]
})

export class AppRoutingModule {

}

这是我的 app.module.ts 代码:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent} from './app.component';
import { AppRoutingModule} from './app-routing.module';
import {RouterModule, Routes} from '@angular/router';
import { TypeaheadComponent } from './shared/typeahead-component';
import { HttpModule} from'@angular/http';
import {DashboardService} from "./services/dashboard-service";



@NgModule({
  declarations: [
    AppComponent,
    TypeaheadComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    RouterModule,
      HttpModule
  ],
  providers: [ RouterModule, DashboardService],
  bootstrap: [AppComponent],
})
export class AppModule { }

这是我的 typeahead-component.ts 相关代码:

  constructor( private route: Route, private router: Router, private dashboardHttpService: DashboardService) {
      this.router.navigate(['typeahead-component/']);
      this.edit();
  }

这是我的dashboard-service.ts 相关代码:

constructor(private http: Http, private route: ActivatedRoute, private router: Router) {
    this.router.navigate(['typeahead-component/']);
  }

此外,我在 tsconfig.app.json、tsconfig.spec.json 和 tsconfig.json 中的 emitDecoratorMetadata 都设置为 true。

提前道歉-我没有为此创建 plunkr,因为我对使用 plunkr 非常陌生。

我将非常感谢我尝试实施的任何工作示例。

谢谢!

4

1 回答 1

0

在组件中,构造函数参数只能是可注入的服务,而 Route 不是。如果您需要当前路由,请像在 dash 组件中那样注入 ActivatedRoute:

constructor( private route: ActivatedRoute, private router: Router, private dashboardHttpService: DashboardService)
于 2017-11-19T00:31:24.430 回答