【问题标题】:Getting Uncaught Error: Can't resolve all parameters for获取未捕获的错误:无法解析所有参数
【发布时间】:2018-12-18 06:16:57
【问题描述】:

我是 Angular 4 的新手,在编译时遇到错误

未捕获的错误:无法解析 AllCountryComponent 的所有参数: ([对象对象],[对象对象],?)。 在 syntaxError (webpack-internal:///./node_modules/@angular/compiler/esm5/compiler.js:707) 在 CompileMetadataResolver._getDependenciesMetadata (webpack-internal:///./node_modules/@angular/compiler/esm5/compiler.js:15927) 在 CompileMetadataResolver._getTypeMetadata (webpack-internal:///./node_modules/@angular/compiler/esm5/compiler.js:15762)

我还附上了错误的截图:


我的 allCountry.component.ts 代码如下

import { Component, OnInit } from '@angular/core';
import { AppService } from '../../app.service';
import { ActivatedRoute, Route } from '@angular/router';
import {Location} from '@angular/common'

@Component({
  selector: 'app-all-country',
  templateUrl: './all-country.component.html',
  styleUrls: ['./all-country.component.css'],
  providers: [Location,AppService]
})
export class AllCountryComponent implements OnInit {
   public name:string;
   public value:string;
   public listCountry:any[];
  constructor(private http:AppService,private _route:ActivatedRoute ,_rout:Route ) {
              console.log("allcountry constuctor are called");
   }

  ngOnInit() {
    this.name=this._route.snapshot.paramMap.get('name');
    this.value=this._route.snapshot.paramMap.get('value');
   console.log(this.name);
   console.log(this.value);
   this.http.getAllCountry(this.name,this.value).subscribe(
     data=>{
       this.listCountry=data;
       console.log(this.listCountry)
     },
    error=>{
      console.log("error occured")
      console.log(error.errorMessege)
    }
   )


  }

}

app.service.ts

import { Injectable } from '@angular/core';
import { HttpClient  } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/toPromise';
import { ApiFormat } from './api-format';

@Injectable()
export class AppService implements ApiFormat {
  public allRegion=[];
  public name:string;
   public value:string;


    public baseUrl="https://restcountries.eu/rest/v2";
  constructor(private http:HttpClient) {
    console.log("service are called");
   }

  public getAllCountry(name:string,value:string):Observable<any>{
       let myResponse = this.http.get(`${this.baseUrl}/${name}/${value}?fields=name;region;capital;currencies;subregion;timezones;population;languages;flag`);
              return myResponse;   
   }

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule }          from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';

import { AppComponent } from './app.component';
import { AppService } from './app.service';
import { SharedModule } from './shared/shared.module';
import { CountryModule } from './country/country.module';
import { HomeComponent } from './home/home.component';
import { AllCountryComponent } from './country/all-country/all-country.component';


@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    AllCountryComponent

  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    SharedModule,
    CountryModule,
    RouterModule.forRoot([
      {path:"home",component:HomeComponent},
      {path:" ",redirectTo:"home",pathMatch:"full"},
      {path:'*',component:HomeComponent},
      {path:'**',component:HomeComponent},
      {path:"allcountry/:name/:value",component:AllCountryComponent}

    ])
  ],
  providers: [AppService],
  bootstrap: [AppComponent]
})
export class AppModule { }

country.module.ts 是

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AllCountryComponent } from './all-country/all-country.component';
import { SigleCountryComponent } from './sigle-country/sigle-country.component';
import { FormsModule } from '@angular/forms';
import { SharedModule } from '../shared/shared.module';
import { RouterModule } from '@angular/router';

@NgModule({
  imports: [
    CommonModule,
    SharedModule,
    FormsModule,
    RouterModule.forChild([
      {path:"allcountry/:name/:value",component:AllCountryComponent},
      {path:"country/:code",component:SigleCountryComponent}
    ])

  ],
  declarations: [AllCountryComponent, SigleCountryComponent]
})
export class CountryModule { }

【问题讨论】:

  • 构造函数中的第三个参数_rout是什么?
  • @cyberpirate92 它只是来自角度路由器的Route。请张贴AppService代码
  • 你没有在任何地方使用_rout,它甚至不是一个班级成员(你可以使用this._rout),为什么还要提供它?
  • @cyberpirate92 我已经给出了名为 app.service.ts 的 appservice 代码

标签: angular typescript rxjs


【解决方案1】:

图片中的错误提示

无法解析 AllCountryComponent 的所有参数:([Object 对象], [对象对象], ?)

注意 ? 它对应于组件构造函数中的第三个参数

constructor(private http:AppService,private _route:ActivatedRoute ,_rout:Route ) {
    console.log("allcountry constuctor are called");
}

不清楚为什么你有第三个参数_rout,而且它不是类成员(因为它没有访问说明符)而且你没有在任何地方使用它。

Route 不能由 Angular 通过 DI 提供。删除第三个参数,它应该可以正常工作。

constructor(private http:AppService,private _route:ActivatedRoute) {
    console.log("allcountry constuctor are called");
}

【讨论】:

  • 删除后再次给出错误,如未捕获错误:类型 AllCountryComponent 是 2 个模块声明的一部分:CountryModule 和 AppModule!请考虑将 AllCountryComponent 移至导入 CountryModule 和 AppModule 的更高模块。您还可以创建一个新的 NgModule,它导出并包含 AllCountryComponent,然后在 CountryModule 和 AppModule 中导入该 NgModule。
  • @ShubhamSingh 这意味着你已经在CountryModuleAppModule 中声明了AllCountryComponent。一个组件只能在一个模块中声明,从另一个模块中删除它
  • 如果您想从AppModule 中删除它,请确保它在app.module.tsdeclarations 属性中不存在
猜你喜欢
  • 2018-08-22
  • 2018-09-21
  • 2023-03-16
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多