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