0

I am importing a class to another class and I want to access its non-static property in Angular but I am unable to do that.

Here is the code that I am using:

Websocket.config.ts

export class WebSocketConfig implements OnInit{
    auth$: Subscription;
    userInfo: UserInfo;
    user_name: string;
    username: string;
    constructor(private authService: AuthorizationService){
    }
    ngOnInit() {
        this.username = this.getUserName();
    }
    getUserName(): string{
        this.auth$ = this.authService.getUserInformation().subscribe(result => {      
            this.userInfo = result;
            this.user_name = this.userInfo.user_id;
          });
          return this.user_name;
    }
    public static uri: string = "wss://localhost:9093/powerme-notification-websocket/websocket";
    public static notification_topic : string = '/user/topic/releaseLock';
}

shared.module.ts

    const stompConfig : StompConfig = {
      url : WebSocketConfig.uri,
      headers: {client_id: WebSocketConfig.username},
      heartbeat_in: 0,
      heartbeat_out: 20000,
      reconnect_delay: 5000,
      debug: false
}

In the WebSocketConfig.username, It says that "Property username does not exist on type "typeOf WebSocketConfig".

I am using Stomp and websocket for sending notifications.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
s4tr2
  • 415
  • 1
  • 9
  • 27
  • 1
    You are confusing class **instances** with class **types**. In your case, you are trying to access a **class type's property**, which should be marked as **static**. In any case, since you're using angular and you're apparently using a component, you should use a **service** instead to accomplish your goal. – briosheje Sep 12 '18 at 13:07
  • Notice that `getUserName` [will always return `undefined`](https://stackoverflow.com/q/23667086/1048572). – Bergi Sep 12 '18 at 13:56

0 Answers0