繁体   English   中英

错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名

[英]ERROR Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias

如何解决这个打字稿错误?

我在下面的行中遇到错误。

常量自我 = 这个;

我在终端中遇到错误,例如:

错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名

请在下面找到代码:

notifyOwner(req, metadata, callback) {
    const currentUserId = req.session.identity.individual.userName;
    const docId = metadata.docId;

    let message = this.templates.selfRemoval;
    const self = this;

    const targetUserId = metadata.ownerId;
    const paperTitle = metadata.title.replace(/<(.|\n)*?>/g, '');
    const nonOwnerRole = metadata.userRole;

    message = message.replace(/\[CollaboratorName\]/g, req.session.identity.individual.firstName + ' ' + req.session.identity.individual.lastName);
    message = message.replace(/\[NonOwnerRole\]/g, (nonOwnerRole === 'author') ? 'collaborator' : 'reviewer');
    message = message.replace(/\[PaperTitle\]/g, paperTitle);

    const eventData = {
      message: message,
      objectType: 'manuscript',
      objectId: docId
    };

    self.createEvent(currentUserId, targetUserId, eventData, (err, result) => {
      if (result) {
        const userActivityService = new UserActivityService();
        userActivityService.broadcastRefreshUserEvents(eventData['objectId'], { userId: targetUserId });
      }

      callback(null, JSON.stringify({ notified: true }));
    });
  }

这个警告通常是为了让脚本编写者使用箭头函数而不是声明新变量。 例如,这个:

let that = this;
someFn(function(arg) {
  return that.foo = arg;
});

可以简化为

someFn(arg => this.foo = arg);

但是在您的情况下,您甚至没有在任何其他函数中使用重新分配的值 - 您只是在代码中直接引用它,这使得分配完全多余。

只需删除您的

const self = this;

并更换

self.createEvent(

this.createEvent(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM