【问题标题】:Call function in function在函数中调用函数
【发布时间】:2023-02-22 22:30:38
【问题描述】:

功能一:

getDescr() {
    this.idResponse = this.descriptor.getDescriptors(this.descriptorSelected).subscribe(
      (data) => {
        this.idResponse = data;
        this.idResponse.values.processingParameters.forEach(function (value: any){
          this.addParameters(value.id);
        });
        console.log;
      },
      error => {
        this.httpErrors.httpErrors.message = error.error.error;
      }
    )
  }

功能二:

addParameters (value: string): void{

    switch (value){
      case 'DOCUMENT_TYPE': {
        console.log('--> DT')
        break;
      }
      case 'EMAIL': {
        console.log('--> EMAIL')
        break
      }
    }

  }

当我在函数 1 中调用 addParameters 时出现错误:

'this' implicitly has type 'any' because it does not have a type annotation

你能告诉我为什么吗?我应该改变什么?

【问题讨论】:

  • edit问题中的代码是一个独立的minimal reproducible example,我们可以将其按原样粘贴到我们自己的 IDE 中,并查看您看到的问题而没有不相关的问题(例如,缺少声明)。否则,您得到的任何答案都必然未经测试,因此不太可能提供帮助。

标签: angular typescript


【解决方案1】:

我不是 100% 确定,但我认为这可能与您在函数 1 中将值声明为任何类型这一事实有关:

 this.idResponse.values.processingParameters.forEach(function (value: any){ // here
          this.addParameters(value.id);
        });

也许您可以尝试更改它以匹配您在 addParameters 函数中设置的字符串类型。

【讨论】:

    【解决方案2】:

    尝试在您的函数中使用“任何”类型来添加它:

     getDescr() {
            this.idResponse = this.descriptor.getDescriptors(this.descriptorSelected).subscribe(
              (data) => {
                this.idResponse = data;
                this.idResponse.values.processingParameters.forEach(function (value: any){
                  this.addParameters(value.id,this);
                });
                console.log;
              },
              error => {
                this.httpErrors.httpErrors.message = error.error.error;
              }
            )
          }
    
    addParameters (this:any, value: string): void{
    
        switch (value){
          case 'DOCUMENT_TYPE': {
            console.log('--> DT')
            break;
          }
          case 'EMAIL': {
            console.log('--> EMAIL')
            break
          }
        }
    
      }
    

    【讨论】:

    • 不,没有帮助
    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多