Sensitive and global variables Ionic 3 and angular 4

Asked

Viewed 3,187 times

1

I have some sensitive variables, and the ideal would be to leave all the variables in one environment, when it is necessary to change, for example from production to development, this task becomes easy.

Ex:

@Injectable()
export class RestaurantesProvider {
private url: string = "http://199.169.9.9/ws/ListarEstados.ashx";
  constructor(private http: Http) {
    console.log(this.url);
  }

getRestaurantes(){
  return this.http.post(this.url, null)
  .do(this.logResponde)
  .map(this.extractData)
  .catch(this.catchError);
}

private catchError(error: Response | any){
  console.log(error);
  return Observable.throw(error.json().error || "Erro de conexção");
}

private logResponde(res: Response){
  console.log(res);
}

private extractData(res: Response){
  return res.json();
}
}

By default I would like to catch the url which in this case is a sensitive variable to use in all my requests, this service and others.

private url: string = padrao + "ListarEstados.ashx";

I’m trying to use the AppModule for this I did, so I did so:

export class AppModule {
   private url: string = "http://199.169.0.9/ws/ListarEstados.ashx";

   getUrl(){
     return this.url;
   }
}

And in my classe I do so:

private url: string = AppModule.getUrl();

The getUrl() is marked and this message appears

[ts] Property 'geturl' does not exist on type 'typeof Appmodule'

2 answers

2


Change your Appmodule object by adding the modifier static:

export class AppModule {
   private static url: string = "http://199.169.0.9/ws/ListarEstados.ashx";

   static getUrl(){
     return this.url;
   }
}

And to receive this data is enough:

private url: string = AppModule.getUrl();

Remembering to instantiate the class, so:

import { AppModule } from '../app/app.module';
  • Be advised not to use this.url method in appModule

  • [ts] Property 'url' does not exist on type 'Appmodule'

  • [ts] Property 'Appmodule' does not exist on type 'Restaurantesprovider'.

  • You imported your Appmodule class? import { AppModule } from './../../model/appModule';

  • It really isn’t that, I tried but it didn’t work

  • It is worth noting that I am taking this data from the service

Show 1 more comment

0

Use

import { MyApp } from '../../app/app.component' 

in place of that import.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.