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

我无法从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”。你能帮助我吗?


杨魅力
浏览 160回答 2
2回答

慕容708150

this由于函数作用域的原因,将失去其引用。您可以使用箭头函数,这样它就可以保留其范围,甚至可以将该isEqualToSelectedText函数移出该类中的私有方法。它可能是这样的:// in the same place you're declaring the functionconst isEqualToSelectedText = (element : History) => {&nbsp; &nbsp; &nbsp; return element.basic_text.basic_text === this.chosedText;}// or moving out to a private method in the classexport class DetailsComponent implements OnInit {&nbsp; &nbsp; history: History[] = [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; basic_text: { basic_text : 'text_1' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; summary: { summary : 'summary_1' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tager: { tag_algorytm : true, tag_text: 'tag_1' }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; basic_text: { basic_text : 'text_2' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; summary: { summary : 'summary_2' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tager: { tag_algorytm : false, tag_text: 'tag_2' }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ];&nbsp; &nbsp; chosedText: string = 'text_1';&nbsp; &nbsp; textFilter(h: History[]) {&nbsp; &nbsp; &nbsp; &nbsp; let result: History[] = [];&nbsp; &nbsp; &nbsp; &nbsp; var param = this.chosedText;&nbsp; &nbsp; &nbsp; &nbsp; var foundIndex= h.findIndex(this.isEqualToSelectedText, param); // <-- changing here&nbsp; &nbsp; &nbsp; &nbsp; result.push(h[foundIndex])&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; // The function was moved out&nbsp; &nbsp; private isEqualToSelectedText(element: History) {&nbsp; &nbsp; &nbsp; &nbsp; return element.basic_text.basic_text === this.chosedText;&nbsp; &nbsp; }}

慕斯王

好的,我已经找到错误了。我在使用“http get”初始化变量之前使用了它。所以问题已经解决了,感谢帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript