鍍金池/ 問(wèn)答/數(shù)據(jù)庫(kù)  HTML/ react this.refs.childMethod is not a fun

react this.refs.childMethod is not a function

react this.refs.method is not a function

父組件

 <Menu.Item key="change_pwd">
    <ChangePwd ref="Changepwd" />
 </Menu.Item>
                  
                  
  handleMenuClick = (item) => {
    console.log(item.key)
    if (item.key == "logout") {
      this.handleLogout()
    }
    if (item.key == "change_pwd") {
      console.log(this.refs.Changepwd)
      // this.refs.Changepwd.childMethod()
    }
  }

子組件

  childMethod() {
    alert("組件之間通信成功");
  }

輸出react this.refs.childMethod is not a function

為啥找不到子組件的方法呢
打印出來(lái)的this.refs.Changepwd如圖并沒(méi)有自己定義的method

clipboard.png

回答
編輯回答
野橘

this.refs.Changepwd 是獲取到的這個(gè)實(shí)體 Dom, 并不是一個(gè)方法。

2017年12月6日 14:34
編輯回答
爆扎

你的ChangePwd是通過(guò)Class聲明的么?看看文檔

2017年12月20日 22:26