4 Replies - 323 Views - Last Post: 19 February 2020 - 08:43 AM

#1 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 163
  • Joined: 24-August 13

Angular class error

Posted 19 February 2020 - 07:34 AM

Hello. I recently started learning Angular and there is a problem I can not solve. I created a class named User with all properties. Then I imported that class in the main class with path. That works fine, no error. I created object of the class, that works fine. But when I want to assign properties in method ngOnInit there is a problem, let me show you both pictures.

Posted Image

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Angular class error

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 638
  • View blog
  • Posts: 4,228
  • Joined: 12-January 10

Re: Angular class error

Posted 19 February 2020 - 08:11 AM

did you hover over the top section where it is underlined in red
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6834
  • View blog
  • Posts: 28,360
  • Joined: 12-December 12

Re: Angular class error

Posted 19 February 2020 - 08:12 AM

Post the actual code and error details, not just screenshots. You should know to do this by now (after 150 posts).
Was This Post Helpful? 0
  • +
  • -

#4 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 163
  • Joined: 24-August 13

Re: Angular class error

Posted 19 February 2020 - 08:36 AM

Sorry anderwsw.

user.model.ts
export class User {
    name: string;
    designation: string;
    address: string;
    phone: string[]
}


address-card-components.ts

import { Component, OnInit, Input } from '@angular/core';
import { User } from './user.model';
@Component({
  selector: 'app-address-card',
  templateUrl: './address-card.component.html',
  styleUrls: ['./address-card.component.css']
})
export class AddressCardComponent implements OnInit {

user: any;
@Input('user') userObj = User;

  constructor() {

   
   }

  ngOnInit(): void {
    this.user = {
      name: this.userObj.name,
      title: this.userObj.designation,
      address:this.userObj.address,    
      phone: this.userObj.phone
    };
  }

}


Errors:

ERROR in src/app/address-card/address-card.component.ts:21:27 - error TS2339: Property 'designation' does not exist on type 'typeof User'.

21 title: this.userObj.designation,
~~~~~~~~~~~
src/app/address-card/address-card.component.ts:22:28 - error TS2339: Property 'address' does not exist on type 'typeof User'.

22 address:this.userObj.address,
~~~~~~~
src/app/address-card/address-card.component.ts:23:27 - error TS2339: Property 'phone' does not exist on type 'typeof User'.

23 phone: this.userObj.phone
~~~~~

Was This Post Helpful? 0
  • +
  • -

#5 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 163
  • Joined: 24-August 13

Re: Angular class error

Posted 19 February 2020 - 08:43 AM

As a solution to this problem Visual Studio offers me to declare properties as static.

This post has been edited by Pogaca: 19 February 2020 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1