我正在嘗試創建一個類組件。我為該類創建了一個介面,但Property '' does not exist on type '' 在我的scrollDiv屬性上出現錯誤。這門課的作文遺漏了什么?
interface ChatTabI {
state: any;
scrollDiv: any;
history: any;
user: any;
}
class ChatTab extends React.Component<ChatTabI> {
// @ts-ignore
constructor(props) {
super(props);
this.state = {
text: "",
messages: [],
loading: false,
channel: null,
};
this.scrollDiv = React.createRef();
}
...
}

uj5u.com熱心網友回復:
this.scrollDiv 您正在使用此行訪問類屬性。
所以你需要做這樣的事情。
interface ChatTabI {
state: any;
scrollDiv: any;
history: any;
user: any;
}
class ChatTab extends React.Component<ChatTabI> {
scrollDiv: any; // or whatever this type would be
constructor(props) {
super(props);
this.state = {
text: "",
messages: [],
loading: false,
channel: null,
};
this.scrollDiv = React.createRef();
}
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338028.html
標籤:javascript 反应 打字稿
下一篇:打字稿如何實作列舉?
