第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在打字稿類中調(diào)用反應(yīng)組件內(nèi)定義的函數(shù)?

如何在打字稿類中調(diào)用反應(yīng)組件內(nèi)定義的函數(shù)?

慕蓋茨4494581 2023-11-02 22:34:40
 export class abc extends React.Component<IProps, IState> {    function(name: string) {    console.log("I wish to call this function"+ name);    }render() {    return (      <div>Hello</div>    );  }}現(xiàn)在我想將上面定義的組件的函數(shù)方法調(diào)用到另一個 xyz.ts 類(不是反應(yīng)組件)中,是否可以做同樣的事情?
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻1788條經(jīng)驗 獲得超4個贊

如果您想在不同的 ts 文件中使用該函數(shù),則絕對應(yīng)該將該函數(shù)移出組件并將其導出。



查看完整回答
反對 回復 2023-11-02
?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

您可以像這樣使用偵聽器模式:


export interface IListener {

    notify: (name:string) => void;

}



export class Listener {

    listener : IListener[] = [];



    addListener(listener: IListener) {

      this.listener.add(listener)

    }


    notifyListener() {

      this.listener.forEach((listener) => {

          listener.notify("abc");

      });

    }

  }


  export class abc extends React.Component<IProps, IState> implements IListener {


    componentDidMount() {

        // register this class as a listener

        new Listener().addListener(this);

    }


    public notify(name:string) {

        this.test(name);

    }


    test(name: string) {

      console.log("I wish to call this function"+ name);

    }

    render() {

      return (

      <div>Hello</div>);

    }

    

  }


查看完整回答
反對 回復 2023-11-02
  • 2 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號