无法在angular 4项目的类中初始化构造函数中的变量

无法在angular 4项目的类中初始化构造函数中的变量,angular,Angular,这是angular 4项目中的我的app.component.ts文件 import { DataService } from './data.service'; import { Component, OnInit } from '@angular/core'; import { Location } from '@angular/common'; @Component({ selector: 'app-root', templateUrl: './app.component.htm

这是angular 4项目中的我的app.component.ts文件

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();
  }
}
在初始化dataservice变量或构造函数中的任何其他变量时,我在浏览器控制台中遇到以下错误

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!"

我认为您的用户可以在App.module.ts文件中将服务定义为提供商


在“提供程序”部分中,添加数据服务并再次尝试构建

我认为您的用户可以在App.module.ts文件中将服务定义为提供程序


在“提供程序”部分中,添加数据服务并重试构建

您没有显示定义和提供数据服务的位置,因此。。。您是否正确定义并提供了它?给出一个。你没有显示你在哪里定义和提供数据服务,所以。。。您是否正确定义并提供了它?给出一个。现在我定义了,但仍然面临着sqme错误@Leo R。现在我定义了,但仍然面临着sqme错误@Leo R。