0

I've tried to make a pretty basic class and for some reason I can't figure out why this variable "doesn't exist"

export class AppModule {
  public static currentHost: string = 'http://localhost:8080/';

  constructor() {
    if (window.location.hostname == "localhost") {
      this.currentHost = "http://" + window.location.hostname + "/";
    }
  }

}

ERROR in src/app/app.module.ts(62,12): error TS2339: Property 'currentHost' does not exist on type 'AppModule'.

Anyone see my error?

Ajmal Sha
  • 906
  • 4
  • 17
  • 36
user3440639
  • 185
  • 2
  • 12

2 Answers2

0

Remove the static keyword. Just do it like this

public  currentHost = 'http://localhost:8080/';

The string type is implied on the value you declared.

brijmcq
  • 3,354
  • 2
  • 17
  • 34
  • What if I want the variable to be static? Will it still behave like a static variable? Kinda like a global variable. – user3440639 Feb 02 '18 at 01:31
  • Why do you need it as a static? You probably came from a C#/Java background like me, check this SO question https://stackoverflow.com/questions/13212521/typescript-static-classes. – brijmcq Feb 02 '18 at 01:35
  • If you want it to be static, call it as `AppModule.currentHost` instead. – John Montgomery Feb 02 '18 at 01:35
  • Or create a separate file and save your constants there something like `export const myConfig = { serverUrl : ' http://localhost:8080/` } And you can use it in your component like this `this.currentHost = myConfig.serverUrl` – brijmcq Feb 02 '18 at 01:37
0

Usually you don't declare variables inside a module ,it should be inside a component which you want to use.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396