【问题标题】:'topFunction' is declared but its value is never read\'topFunction\' 已声明,但其值从未被读取
【发布时间】:2018-09-28 18:01:31
【问题描述】:

在 Angular 项目中,我在我的 html 页面中给出了一个名为“topFunction”的函数名称,并在 component.ts 中的 ngOnit() 中编写了该函数

ngOnInit() {

  function topFunction() {
    ...
  }

}

将鼠标悬停在函数名称上时,它显示文本“'topFunction' 已声明,但它的值从未被读取。 (局部函数) topFunction(): void “该特定功能不起作用。

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    您只声明了一个函数。除非你调用它,否则它的代码将永远不会被执行,因此它是未被使用的。 Typescript 会警告您未使用的本地人(在 strictnoUnusedLocals 下)。

    要调用该函数,您应该编写

    ngOnInit() {
    
      function topFunction() {
        ...
      }
      topFunction();
    }
    

    或者用它做其他事情,例如将其分配给成员或事件处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 2020-01-08
      • 2018-10-05
      • 2023-03-15
      相关资源
      最近更新 更多