0

I am working on an Angular 7 app. I have created a class/Model UserRoleDto in TypeScript as:

export default class UserRoleDto
{
    static userName: string;
    static userRole: string;
}

I am using it in a component like this:

import  UserRoleDto  from '../models/dto/user-role-dto';

export class SettingDataComponent implements OnInit {
   showAnalytics : boolean = false;
   constructor() {
      if(UserRoleDto.userRole === "Admin")
          this.showAnalytics = true;
  }

   ngOnInit() {
  }
}

I am getting exception at run-time, the exception is:

ReferenceError: UserRoleDto is not defined

What am i doing wrong ? I know i am making some very silly mistake but, any help would be much appreciated.

2
  • @Ramesh i am, i have updated the question Commented Jan 20, 2020 at 11:41
  • I am even unable to access UserRoleDto in console @Ramesh Commented Jan 20, 2020 at 11:46

3 Answers 3

1

You are trying to access properties of the class definition. What you want is to access the properties of the class instance. To instanciate a class, you have to use the new keyword:

const userRole = new UserRoleDto();

This executes the classes constructor function and will create an instance of your class which is stored in the userRole variable. You can have multiple instances of your class. You can access public properties of your class and (if they are not readonly) you can assign values to them.

userRole.userRole = 'Admin';
console.log(userRole.userRole) // prints 'Admin'

By default your class properties do not have any values assigned if you do not define them in your class. You can do so by

export class UserRoleDto {
  userRole = 'Admin';
}

Angular Dependency Injection

The Angular dependency injection is able to construct your classes intelligently without the need to instantiate them manually. If these classes are known by the DI of the corresponding module, you can inject them by defining parameters of your constructor.

I think what you want is a service (a class known by the DI) which can store data module-wide, not component wide.

import { Injectable } from '@angular/core';

@Injectable()
export class UserRoleService {
  userRole = 'Admin';
}

In your module where you also declare your component you have to define it in the providers array.

@NgModule({
  ...
  providers: [
    UserRoleService
  ]
})

And in your component you can then inject the service as follows

export class SettingDataComponent {
  showAnalytics = false;

  constructor(private userRoleService: UserRoleService) {
    if(userRoleService.userRole === "Admin")
      this.showAnalytics = true;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are not defined your UserRoleDto in your SettingDataComponent component. I am not sure where you get UserRoleDto values. Don't forget to assign it before checking user role.

import  UserRoleDto  from '../models/dto/user-role-dto';

    export class SettingDataComponent implements OnInit {
       showAnalytics : boolean = false;
      userRoleDto: UserRoleDto;
       constructor() {
          if(this.userRoleDto.userRole === "Admin")
              this.showAnalytics = true;
      }

       ngOnInit() {
      }
    }

Comments

0

You created a model so that you can tell typescript that you will have some variables in your app with that type but you are using it incorrectly.

Consider this:

import  UserRoleDto  from '../models/dto/user-role-dto';

export class SettingDataComponent implements OnInit {

   showAnalytics : boolean = false;
   constructor() {
      let someUserVariable: UserRoleDto;
      // In this way you can use the model to tell typescript that your variable is of type "UserRoleDto" instead of ": any".

      // Now you can assign it to an object that obeys that type and do what you want. Of course, the variables and their scope depend on your requirements.
      someUserVariable = {
       userName: 'adminUser',
       userRole: 'Admin'
      }

      if(someUserVariable.userRole === "Admin") {
       this.showAnalytics = true;
      } 
  }

   ngOnInit() {
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.