How can I extract AngularJS constants in a Typescript function?

My code has the following:

.service('testService', TestService) .service('userService', UserService) .constant("appConstant", { appName: "My App", appVersion: 2.0, baseUrl: "http://localhost:3048", Action: { None:0, Registering: 1, Authenticating: 2 } }); 

Is there a way I can extract a persistent object into another .ts file and include it?

+5
source share
1 answer

Yes. You may have a config in an external file. The easiest approach is to do something like this:

 class Constants { static get Default():any { return { appName: "My App", appVersion: 2.0, baseUrl: "http://localhost:3048", Action: { None:0, Registering: 1, Authenticating: 2 } }; } } 

And then you can access it through Constants.Default . So your constant method in angular would look like this:

 .constant("appConstant", Constants.Default); 

More Typescript will return types instead of the object literal in the default get () method. So you can go and reorganize my example if you want.

+8
source

Source: https://habr.com/ru/post/1209122/


All Articles