首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >错误TS2554:应为1个参数,但实际为0。Angular6

错误TS2554:应为1个参数,但实际为0。Angular6
EN

Stack Overflow用户
提问于 2018-09-06 03:08:51
回答 5查看 15.6K关注 0票数 8

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

employee.service.ts:

代码语言:javascript
复制
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文件是这样的:

代码语言:javascript
复制
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

代码语言:javascript
复制
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的新手,所以我找不到这个错误,请解释一下。谢谢。

EN

回答 5

Stack Overflow用户

发布于 2019-11-08 15:26:56

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

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

代码语言:javascript
复制
{  
  "extends": "../tsconfig.json",  
  "compilerOptions": {  
    "outDir": "../out-tsc/app",  
    "baseUrl": "./",  
    "module": "es2015",  
    "types": []  
  },

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

Stack Overflow用户

发布于 2019-10-22 19:35:45

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

代码语言:javascript
复制
private configService= new ConfigService();

其中ConfigService按照演示使用HttpClient

我在我的组件中使用new构造服务,它需要HttpClient作为参数来实例化服务。

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

更改为此选项,错误就会消失。注意添加的行提供程序: ConfigService

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

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2020-12-29 16:32:59

有时你在一个函数中遗漏了一个参数,在本地构建时它可以很好地工作,但对于生产构建,它将查找该参数并在缺少参数时抛出错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52191925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档