如何将服务中定义的类用作组件?

时间:2018-10-08 07:34:52

标签: angular typescript

如何将服务中定义的类用作组件? Service.ts文件包含一个我想在组件中使用的类。 我已将服务注入组件中,但仍然无法使用该类。

**Here's my service.ts file


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

    @Injectable({
      providedIn: 'root'
    })
    class Registration{
      constructor(
       public id: number = null,
      public name: string = '',
      public age:number=null,
      public dept: string = 'Select Your Department',
      public  empType: string = 'Select Employee Type',
      public hra: string = '',
      public medical:string='',
      ) {}
      }

    export class DataService {
      registrations: Registration[] = [];
      myModal: Registration;
      showNew: Boolean = false;
      submitType: string = 'Save';
      selectedRow: number;
      empType: string[] = ['Permanent','Trainee'];

      constructor() { }
    }

//这是我的组件文件,我要在其中使用要在组件中使用的注册类。

import { Component, OnInit, ViewChild } from '@angular/core';
import { DataService } from '../data.service';



@Component({
  selector: 'app-registration',
  templateUrl: './registration.component.html',
  styleUrls: ['./registration.component.css']
})
export class RegistrationComponent implements OnInit {
  @ViewChild('myModal') myModal: any;
  // It maintains list of Registrations
  registrations: Registration[] = [];
  // It maintains registration Model
  regModel: Registration;
  // It will be either 'Save' or 'Update' based on operation.
  submitType: string = 'Save';
  // It maintains table row index based on selection.
  selectedRow: number;
  // It maintains Array of Types.
  employeeType: string[] = ['Permanent', 'Trainee'];
  //It maintains the department Type
  department: string[] = ['PES', 'AP1', 'AP2', 'CSD', 'QSD', 'ADMIN'];
  constructor(private data: DataService) {
    // Add default registration data.
    //this.registrations.push(new Registration('Johan', 28, 'PES', 'Permanent', '5', '10'));

  }

  ngOnInit() { }






  // This method associate to New Button.
  onNew() {
    // Initiate new registration.
    this.regModel = new Registration();
    // Change submitType to 'Save'.
    this.submitType = 'Save';
    // display registration entry section.
   // this.showNew = true;
  }







  // This method associate to Save Button.
  onSave() {

    if (this.submitType === 'Save') {

      // Push registration model object into registration list.
      (() => {
        // Whatever is here will be executed as soon as the script is loaded.
        confirm("Press OK , If You Want To Save !");
        console.log('Data Saved');
        this.myModal.nativeElement.click();
      })();

      this.registrations.push(this.regModel);

    } else {
      // Update the existing properties values based on model.
      this.registrations[this.selectedRow].name = this.regModel.name;
      this.registrations[this.selectedRow].age = this.regModel.age;
      this.registrations[this.selectedRow].dept = this.regModel.dept;
      this.registrations[this.selectedRow].empType = this.regModel.empType;
      this.registrations[this.selectedRow].hra = this.regModel.hra;
      this.registrations[this.selectedRow].medical = this.regModel.medical;

      if(this.myModal)
        this.myModal.nativeElement.click();
    }
    // Hide registration entry section.
    //this.showNew = false;
  }





  // This method associate to Edit Button.
  onEdit(index: number) {
    // Assign selected table row index.
    this.selectedRow = index;
    // Initiate new registration.
    this.regModel = new Registration();
    // Retrieve selected registration from list and assign to model.
    this.regModel = Object.assign({}, this.registrations[this.selectedRow]);
    // Change submitType to Update.
    this.submitType = 'Update';
    //this.showNew = true;
  }





}**

1 个答案:

答案 0 :(得分:0)

您必须使用export关键字导出类。与您为DataService做的一样。

不过,您应该将@Injectable移到注册类下方。我个人认为最好是每个类定义使用单独的文件:

export class Registration {
  // ...
}

@Injectable({
  providedIn: 'root'
})
export class DataService {
  // ...
}