Home > other >  how to use service in a constructor of a class that use this service
how to use service in a constructor of a class that use this service

Time:01-04

I don't succeed writing a component using Services( Injectable). The compiler doesn't alert on the problem and it's very strange. When I write this component (that use the service) I see nothig on the screen.

I try to write inside the ctor of the class that use the service a variable from the service type and it cause the problem. It seen like that:

import { Component, OnInit } from '@angular/core';
import Customer from 'src/Models/Customer';
import CustomerService from '../Services/Customer.service';
import { LogService } from '../Services/Log.service';

@Component({
  selector: 'app-customers-list',
  templateUrl: './customers-list.component.html',
  styleUrls: ['./customers-list.component.scss']
})
export class CustomersListComponent implements OnInit {
  cArr: Customer[]
  constructor(private customerSer:CustomerService,private log:LogService) {
    this.cArr = this.customerSer.arr
  }
    // public customerSer: CustomerService
  ngOnInit(): void {
  }

}

and this is the Service class:

import { Injectable } from "@angular/core";
import Customer from "src/Models/Customer";
import { LogService } from "./Log.service";
@Injectable({providedIn:"root"})
export default class CustomerService {
    constructor(public logSer: LogService) { }
    //
    arr: Customer[] = [
        new Customer("159487", "Moshe", 6199044, "satisfied            
  • Related