【问题标题】:Use rest parameters instead of arguments - Angular ES6使用剩余参数而不是参数 - Angular ES6
【发布时间】:2018-10-17 14:23:06
【问题描述】:

我正在尝试使用箭头方法访问嵌套函数中的组件 this。当我使用箭头方法时,我得到一个编译器错误“错误 TS2496:在 ES3 和 ES5 的箭头函数中无法引用 'arguments' 对象。考虑使用标准函数表达式。”

我读过它并试图将其转换为休息参数,但不知道如何让它工作。

休息前参数:

    ((H => {
        H.wrap((H as any).seriesTypes.sunburst.prototype, 'drillUp', (proceed) => {

            proceed.apply(this, Array.prototype.slice.call(arguments, 1));

            console.log('drillup');
                 this.drillUpClick();

        });
    }))(Highcharts);

休息后参数:

    ((H => {
        H.wrap((H as any).seriesTypes.sunburst.prototype, 'drillUp', (proceed, ...args) => {

            proceed.apply(this, Array.prototype.slice.call(args, 1));

            console.log('drillup');
            this.drillUpClick();

        });
    }))(Highcharts);

我无法启动 console.log。收到错误“无法读取未定义的未定义属性”

请指教。

【问题讨论】:

  • proceed.apply(this, args);
  • 不。没用。还是一样的错误。
  • 不,但现在可能正在对this 唠叨,箭头功能也没有。并且没有其他选择。你需要使用普通的function(proceed, ...args){ proceed.apply(this, args); ... }
  • 听起来像这样。现在向上钻取功能停止工作。
  • 向我们展示对drillUp函数的调用

标签: javascript angular ecmascript-6


【解决方案1】:

args 已经是你的其余参数了。所以不要对数组进行切片:

((H => {
    H.wrap((H as any).seriesTypes.sunburst.prototype, 'drillUp', (proceed, ...args) => {
        if(args && args.length > 0) {
            proceed.apply(this, args);
        }
        console.log('drillup');
        this.drillUpClick();

    });
}))(Highcharts);

【讨论】:

  • 确保args是用if(args && args.length > 0)定义的
  • 记录的 args 并返回一个空数组。
  • if (args && args.length > 0) { console.log(args) } 它根本不打印 args 参数。
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 2020-06-09
  • 2019-12-31
  • 2016-12-09
  • 2018-10-12
  • 2014-01-31
  • 2019-08-02
相关资源
最近更新 更多