繁体   English   中英

属性 '' 不存在于类型 '' - 尽管有接口

[英]Property '' does not exist on type '' - despite having interface

我正在尝试创建一个类组件。 我为该类创建了一个接口,但在我的scrollDiv属性Property '' does not exist on type ''上出现错误Property '' does not exist on type '' 这门课的作文遗漏了什么?

 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();

 }
...
}

代码截图

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();

 }
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM