简体   繁体   中英

turn static method of class into variable typescript

Hi all so currently I am making a service for my application/library i am creating and so far this is the best way I can figure out how to create a service. Ideally I would like to change this method into a variable for easier reading

 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; }; }

currently i have this

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

but would love for it to read like this

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

This is called a getter :

export default class NavigationService {

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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