Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import { ENV_PROVIDERS } from './environment';
  2. import { APP_RESOLVER_PROVIDERS } from './app.resolver';
  3.  
  4. // Application wide providers
  5. const APP_PROVIDERS = [
  6. ...APP_RESOLVER_PROVIDERS,
  7. AppState,
  8. AppConfig
  9. ];
  10. @NgModule({
  11. bootstrap: [ App ],
  12. declarations: [ ... ],
  13. imports: [ ... ],
  14. providers: [
  15. ENV_PROVIDERS,
  16. APP_PROVIDERS
  17. ]
  18. })
  19. export class AppModule {
  20.  
  21. import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  22. import { Injectable } from '@angular/core';
  23. import { Observable } from 'rxjs/Observable';
  24. import 'rxjs/add/observable/of';
  25.  
  26. @Injectable()
  27. export class DataResolver implements Resolve<any> {
  28. constructor() {
  29.  
  30. }
  31. resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  32. return Observable.of({ res: 'I am data'});
  33. }
  34. }
  35.  
  36. // an array of services to resolve routes with data
  37. export const APP_RESOLVER_PROVIDERS = [
  38. DataResolver
  39. ];
  40.  
  41. // Angular 2
  42. // Environment Providers
  43. let PROVIDERS: any[] = [
  44. // common env directives
  45. ];
  46.  
  47. if ('production' === ENV) {
  48. PROVIDERS = [
  49. ...PROVIDERS,
  50. // custom providers in production
  51. ];
  52.  
  53. } else {
  54. // Development
  55. PROVIDERS = [
  56. ...PROVIDERS,
  57. // custom providers in development
  58. ];
  59.  
  60. }
  61.  
  62. export const ENV_PROVIDERS = [
  63. ...PROVIDERS
  64. ];
  65.  
  66. @NgModule({
  67. imports: [ CommonModule, HttpModule ],
  68. declarations: [],
  69. exports: [],
  70. providers: [ UsersService ]
  71. })
  72. export class ApiModule {
  73. public static forConfig(configuration: Configuration): ModuleWithProviders {
  74. return {
  75. ngModule: ApiModule,
  76. providers: [ {provide: Configuration, useValue: configuration}]
  77. }
  78. }
  79. }
  80.  
  81. export interface ConfigurationParameters {
  82. apiKey?: string;
  83. username?: string;
  84. password?: string;
  85. accessToken?: string;
  86. basePath?: string;
  87. }
  88.  
  89. export class Configuration {
  90. apiKey: string;
  91. username: string;
  92. password: string;
  93. accessToken: string;
  94. basePath: string;
  95.  
  96.  
  97. constructor(configurationParameters: ConfigurationParameters = {}) {
  98. this.apiKey = configurationParameters.apiKey;
  99. this.username = configurationParameters.username;
  100. this.password = configurationParameters.password;
  101. this.accessToken = configurationParameters.accessToken;
  102. this.basePath = configurationParameters.basePath;
  103. }
  104. }
  105.  
  106. @Injectable()
  107. export class UsersService {
  108. protected basePath = 'http://localhost/commty/cmng';
  109. public defaultHeaders: Headers = new Headers();
  110. public configuration: Configuration = new Configuration();
  111.  
  112. constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement