亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用“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 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號