繁体   English   中英

错误 TS2554:需要 1 个参数,但得到 0。Angular6

[英]error TS2554: Expected 1 arguments, but got 0. Angular6

我有这个问题,我的应用程序工作正常,但此错误显示在 IDE 中。

员工.service.ts:

import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
getEmployee(){
  return [
    {"id":1,"name":"Micheal","age":21},
    {"id":2,"name":"Jackson","age":22},
    {"id":3,"name":"Hales","age":23},
    {"id":4,"name":"Moz","age":25}
  ];
}
  constructor() { }
}

我的employeeDetail.ts 文件是这样的:

import { Component, OnInit } from '@angular/core';
import { EmployeeService } from '../services/employee.service';

@Component({
  selector: 'employee-detail',
  templateUrl: './employee-detail.component.html',
  styleUrls: ['./employee-detail.component.css']
})
export class EmployeeDetailComponent implements OnInit {

  public employees = [];
  constructor(private _employeeService : EmployeeService) {

   }

  ngOnInit() {
    this.employees = this._employeeService.getEmployee();
  }

}

这是 Employee-list.ts

import { Component, OnInit } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import { EmployeeService } from '../services/employee.service';

@Component({
  selector: 'employee-list',
  templateUrl: './employee-list.component.html',
  styleUrls: ['./employee-list.component.css']
})
export class EmployeeListComponent implements OnInit {

  public employees = []



  constructor(private _employeeService : EmployeeService) { 


  }

  ngOnInit() {
    this.employees = this._employeeService.getEmployee();
  }

}

我怎么解决这个问题? 实际问题是什么? 因为我是 angular6 的新手,所以我无法弄清楚错误,请解释一下。 谢谢你。

我没有看到您的代码有任何问题,因为我认为您应该使用Ctrl + C停止应用程序

然后做ng serve

无论如何原因是,您的 getEmployee 方法仍然需要传递一个参数,只需确保您的代码保存在服务部分。

似乎与新的Angular编译器选项fullTemplateTypeCheck有关,如果在tsconfig文件中将该选项设置为true,则会出现错误。

以下是重现构建错误的 tsconfig.app.json 文件示例:

{  
  "extends": "../tsconfig.json",  
  "compilerOptions": {  
    "outDir": "../out-tsc/app",  
    "baseUrl": "./",  
    "module": "es2015",  
    "types": []  
  },

  "angularCompilerOptions": {  
     "fullTemplateTypeCheck": true  
  },  
  "exclude": [  
    "test.ts",  
    "**/*.spec.ts"  
  ]  
}

使用 Angular8,我在这一行有相同的错误触发器:

private configService= new ConfigService();

根据演示,ConfigService 使用HttpClient

我正在使用 new 在我的组件内部构建服务,这需要 HttpClient 作为参数来实例化服务。

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css' ],
})
export class MainComponent {
  private dataService= new ConfigService();
}

改成这个,错误就消失了。 注意线路提供者的添加:[ConfigService]

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css' ],
  providers: [ConfigService]
})
export class MainComponent {
  private dataService: ConfigService;
}

希望这可以帮助。

这是一个有效的错误,并不是由fullTemplateTypeCheck编译选项引起的。 当您创建一个类使用 Angular 可注入对象的实例时,就会发生这种情况。 如果您改用该类作为可注入对象,问题就解决了。

这是一个工作示例。 假设我们有一个如下所示的服务类:

服务等级

/**
 * First Service
 */
import { Injectable } from '@angular/core';
import { SecondService } from './second-service';

@Injectable({
  providedIn: 'root',
})
export class FirstService {
  constructor(public secondService: SecondService) {
    ...
  }
}

如果您尝试在具有new FirstService()另一个服务或组件中使用它,您将看到错误。

破碎的

/**
 * Third Service
 */
import { Injectable } from '@angular/core';
import { FirstService } from './first-service';

@Injectable({
  providedIn: 'root',
})
export class ThirdService {
  firstService: any;

  constructor() {
    this.firstService = new FirstService();
  }
}

错误error TS2554: Expected X arguments, but got 0.发生是因为如果您尝试像这样直接实例化可注入类确实有参数。 如果您将其用作 Angular 可注入对象,则不再是问题。

固定的

/**
 * Third Service
 */
import { Injectable } from '@angular/core';
import { FirstService } from './first-service';

@Injectable({
  providedIn: 'root',
})
export class ThirdService {
  constructor(public firstService: FirstService) {
    ...
  }
}

使用FirstService作为 Angular @Injectable解决了这个问题。

@intotecho 的回答对确定这一点非常有帮助。

有时您在函数中丢失了一个参数,它在本地构建时可以正常工作,但对于生产构建,它会查找该参数并引发缺少参数的错误。

这是来自我的 Angular 11 经验,尽管该解决方案可能适用于其他版本。 当我的标记没有传递任何参数时,我不小心在我的打字稿中向onSubmit()添加了一个参数。

我所要做的就是从onSubmit()删除未使用/不必要的参数。

通过运行npm uninstall -g @angular/cli卸载 Angular CLI
通过运行npm cache clean --force清除npm cache clean --force
并通过运行npm install -g @angular/cli重新安装 CLI

然后从干净的目录创建一个新的应用程序并确保所有版本都是最新的

Angular CLI: 6.1.2
Node: 10.7.0
OS: win32 x64
Angular: 6.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.2
@angular-devkit/build-angular     0.7.2
@angular-devkit/build-optimizer   0.7.2
@angular-devkit/build-webpack     0.7.2
@angular-devkit/core              0.7.2
@angular-devkit/schematics        0.7.2
@ngtools/webpack                  6.1.2
@schematics/angular               0.7.2
@schematics/update                0.7.2
rxjs                              6.2.2
typescript                        2.7.2
webpack                           4.9.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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