为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用“findIndex”函数访问类变量 - 已解决

无法使用“findIndex”函数访问类变量 - 已解决

杨魅力 2023-11-02 22:50:00
我无法从findIndex方法访问我的类变量。 My whole class:    import { Component, Input, OnInit } from '@angular/core';import { History } from '../model/history';import { SharedService } from "../shared.service";@Component({  selector: 'app-details',  templateUrl: './details.component.html',  styleUrls: ['./details.component.scss']})export class DetailsComponent implements OnInit {  history: History[] = [    {      basic_text: { basic_text: 'text_1' },      summary: { summary: 'summary_1' },      tager: { tag_algorytm: true, tag_text: "tag_1" }    },    {      basic_text: { basic_text: 'text_2' },      summary: { summary: 'summary_2' },      tager: { tag_algorytm: false, tag_text: "tag_2" }    }  ];  basic_text: String = "Tekst podstawowy";  summary: String = "Streszczenie";  rating: String = "Ocenione zdania";  chosedText: string;  asd: string = 'asdad';  constructor(private sharedService: SharedService) { }  ngOnInit() {    this.sharedService.sharedMessage.subscribe(chosedText => this.chosedText = chosedText)  }  textFilter(h: History[]) {    let result: History[] = [];    var param = this.chosedText;    var foundIndex = h.findIndex(this.isEqualToSelectedText, param);    result.push(h[foundIndex])    return result;  }  // The function was moved out  private isEqualToSelectedText(element: History) {    return element.basic_text.basic_text === this.chosedText;  }}但如果我这样做var x= h.findIndex(isEqualToSelectedText, 'text_1');一切正常。我尝试从 isEqualToSelectedText 函数访问我的类 varialb 。但我总是遇到这个错误。在findIndex运行期间,单词“this”应等于“text_1”。你能帮助我吗?
查看完整描述

2 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

this由于函数作用域的原因,将失去其引用。您可以使用箭头函数,这样它就可以保留其范围,甚至可以将该isEqualToSelectedText函数移出该类中的私有方法。


它可能是这样的:


// in the same place you're declaring the function

const isEqualToSelectedText = (element : History) => {

      return element.basic_text.basic_text === this.chosedText;

}

// or moving out to a private method in the class

export class DetailsComponent implements OnInit {

    history: History[] = [

        {

            basic_text: { basic_text : 'text_1' },

            summary: { summary : 'summary_1' },

            tager: { tag_algorytm : true, tag_text: 'tag_1' }

        },

        {

            basic_text: { basic_text : 'text_2' },

            summary: { summary : 'summary_2' },

            tager: { tag_algorytm : false, tag_text: 'tag_2' }

        }

    ];


    chosedText: string = 'text_1';


    textFilter(h: History[]) {

        let result: History[] = [];

        var param = this.chosedText;

        var foundIndex= h.findIndex(this.isEqualToSelectedText, param); // <-- changing here


        result.push(h[foundIndex])

        return result;

    }


    // The function was moved out

    private isEqualToSelectedText(element: History) {

        return element.basic_text.basic_text === this.chosedText;

    }

}


查看完整回答
反对 回复 2023-11-02
?
慕斯王

TA贡献1864条经验 获得超2个赞

好的,我已经找到错误了。我在使用“http get”初始化变量之前使用了它。所以问题已经解决了,感谢帮助。



查看完整回答
反对 回复 2023-11-02
  • 2 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信