大家好,目前我正在为要创建的应用程序/库提供服务,到目前为止,这是我弄清楚如何创建服务的最佳方法。理想情况下,我想将此方法更改为变量以便于阅读



import { NavigationRoute, NavigationParams } from 'react-navigation';
import { NavigationStackProp } from 'react-navigation-stack';

type Navigation =
  | NavigationStackProp<NavigationRoute<NavigationParams>, NavigationParams>
  | undefined;

export default class NavigationService {
  private static _navigator: Navigation;

  public static setNavigator(navigatorRef: Navigation) {
    this._navigator = navigatorRef;
  }

  public static navigate = (): Navigation => {
    // TODO: look into how to make this a variable
    return NavigationService._navigator;
  };
}





目前我有这个


      IconPress: () => NavigationService.navigate()?.openDrawer()





但希望它能像这样阅读



      IconPress: () => NavigationService.navigate?.openDrawer()

最佳答案

这称为getter

export default class NavigationService {

    public static get navigate(): Navigation {
        return NavigationService._navigator;
    }
}

09-28 11:47