将类的静态方法转换为变量打字稿

问题描述 投票:0回答:1

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

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()
javascript reactjs typescript react-native service
1个回答
1
投票

这称为getter

export default class NavigationService {

    public static get navigate(): Navigation {
        return NavigationService._navigator;
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.