为什么我要继续获取在我的tsx文件中,类方法'componentDidMount'必须是'私有''公共'或'受保护'警告?

Ami*_*mir 14 typescript reactjs

我不确定我应该在反应类组件中标记我的方法.我在这些方法上收到此错误:componentDidMount,componentDidUpdate,componentWillUpdate和render

这是我的基本组件:

import * as React from 'react';

const { Component } = React;

export default class Loading extends Component<{}, {}>  {
  componentDidMount() {
    console.log('....something....');
  }
  componentDidUpdate() {
    console.log('....something....');
  }
  componentWillUpdate() {
    console.log('....something....');
  }

  render() {
    const style = {
      background: '#f5f5f5',
      height: '100%',
      padding: '20px',
      textAlign: 'center',
      transition: 'all 0.5s linear',
      width: '100%'
    };
    return (
      <div id='app-loader' className='rounded' style={style}>
        <div className='loader large block rounded'>Loading...</div>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我不能把私有render()等因为破坏组件.

Dav*_*ret 24

这是tslint 成员访问规则.

tslint.json中,更改:

"member-access": true
Run Code Online (Sandbox Code Playgroud)

至:

"member-access": [true, "no-public"] // Or false. Read the rule and see what you want.
Run Code Online (Sandbox Code Playgroud)

  • 我个人从未将此设置为"真实".这是浪费空间,并且当缺少访问修饰符时显而易见,指定`public`会增加混乱. (6认同)