【问题标题】:ERROR Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名
【发布时间】:2022-07-07 14:17:34
【问题描述】:

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

下面一行出现错误。

const self = this;

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

错误 '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 }));
    });
  }

【问题讨论】:

    标签: angular typescript eslint lint tslint


    【解决方案1】:

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

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

    可以简化为

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

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

    只需删除您的

    const self = this;
    

    替换

    self.createEvent(
    

    this.createEvent(
    

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 2021-05-31
      • 2020-07-07
      • 2016-03-01
      • 2019-06-30
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多