public static const in TypeScript

前端 未结 8 843
慢半拍i
慢半拍i 2020-12-07 10:11

Is there such a thing as public static constants in TypeScript? I have a class that looks like:

export class Library {
  public static BOOK_SHELF_NONE: stri         


        
相关标签:
8条回答
  • 2020-12-07 10:59

    Just simply 'export' variable and 'import' in your class

    export var GOOGLE_API_URL = 'https://www.googleapis.com/admin/directory/v1';
    
    // default err string message
    export var errStringMsg = 'Something went wrong';
    

    Now use it as,

    import appConstants = require('../core/AppSettings');
    console.log(appConstants.errStringMsg);
    console.log(appConstants.GOOGLE_API_URL);
    
    0 讨论(0)
  • 2020-12-07 11:05

    You can do it using namespaces, like this:

    export namespace Library {
        export const BOOK_SHELF_NONE: string = 'NONE';
    }
    

    Then you can import it from anywhere else:

    import {Library} from './Library';
    console.log(Library.BOOK_SHELF_NONE);
    

    If you need a class there as well include it inside the namespace: export class Book {...}

    0 讨论(0)
提交回复
热议问题