Moulali
Moulali

Reputation: 465

Unable to initialize variables in constructor in a class in angular 4 project

this is my app.component.ts file in angular 4 project.

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {

  submitted = false;

  ngOnInit() {
  }

  constructor(private dataService: DataService) { }

  private generateOtp(): void {
    // this.dataService.create(this.customer);
  }

  onSubmit() {
    this.submitted = true;
    this.generateOtp();
  }
}

While initializing dataservice variable or any other variable in constructor i'm getting this following error in browser console

fileName: "http://localhost:4200/vendor.bundle.js line 569 > eval"
lineNumber: 1208
message: "StaticInjectorError(AppModule)[AppComponent -> DataService]: \n  StaticInjectorError(Platform: core)[AppComponent -> DataService]: \n    NullInjectorError: No provider for DataService!"

Upvotes: 0

Views: 66

Answers (1)

Léo R.
Léo R.

Reputation: 2698

I think your forgot define your service as a provider in your App.module.ts file.

In the Provider section add DataService an try build again

Upvotes: 2

Related Questions