【问题标题】:Error: Void function return value is used错误:使用了无效函数返回值
【发布时间】:2021-07-15 06:51:08
【问题描述】:

我在 Symfony 5 中使用 Stimulus,当我在我的 Stimulus 控制器中使用此代码时,PhpStorm 显示错误。

我该如何解决这个问题?

import {Controller} from 'stimulus';
import {index} from "../js/index";
export default class extends Controller{
    connect(){
        const index1 = index();
    }
}

错误:使用了无效函数返回值

我的index() 函数是:

export function index(){
...
}

【问题讨论】:

  • 索引是否返回任何内容?如果没有,你可以删除const index1 = ,因为无论如何调用index()时它不会设置任何东西,所以没有什么可以分配

标签: javascript symfony ecmascript-6 phpstorm stimulusjs


【解决方案1】:

使用此代码:

import {Controller} from 'stimulus';
import Index from "../js/index";
export default class extends Controller{
    connect(){
        const index1 = new Index;
        index1.index();
    }
}

并将其用于您的索引类:

export default class Index {
    index(){
        ...
    }
}

【讨论】:

  • index();替换const index1 = index();不是更简单吗?为什么需要上课?
  • @FelixKling 当你创建一个类并想在其他类中使用时,你需要使用const index1 = new Index
  • 所有我看到的用户想要调用一个函数,而不是实际使用一个类。但也许我不明白这是怎么回事。
  • @BahmanMD 你能解释一下为什么你的工作有效吗?
猜你喜欢
  • 2020-04-10
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 2011-03-15
  • 2020-11-22
  • 2019-05-02
相关资源
最近更新 更多