鍍金池/ 問答/數(shù)據(jù)庫  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

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

clipboard.png

回答
編輯回答
野橘

this.refs.Changepwd 是獲取到的這個實體 Dom, 并不是一個方法。

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

你的ChangePwd是通過Class聲明的么?看看文檔

2017年12月20日 22:26