src / app / app.module.ts(15,15)中的错误:错误TS2304:找不到名称“ ServiceName”

时间:2019-02-14 17:58:49

标签: javascript angular typescript angular-cli angular7

我是新手,尝试逐步学习每一步,我提供一项服务,需要在Component中使用它,但遇到上述错误。

  1. 使用角度cli ng new appName创建了一个应用
  2. 使用ng g c comp-name
  3. 创建了一个组件
  4. 使用ng g s service-name
  5. 创建了一项服务
  6. 当我在提供者内部的app.module.ts中添加服务名称时:['service-name']。使用ng build || ng serve

    构建代码后出现以下错误
    Hash: 31c1abeb694df29adcbe
    

    时间:8299ms 块{es2015-polyfills} es2015-polyfills.js,es2015-polyfills.js.map(es2015-polyfills)284 kB [初始] [呈现] 块{main} main.js,main.js.map(main)637字节[初始] [呈现] 块{polyfills} polyfills.js,polyfills.js.map(polyfills)93.1 kB [初始] [呈现] 块{runtime} runtime.js,runtime.js.map(运行时)6.08 kB [entry] [rendered] 块{styles} styles.js,styles.js.map(样式)16.3 kB [initial] [rendered]

    src / app / app.module.ts(15,15)中的错误:错误TS2304:找不到名称“ ServiceOneService”。

组件类:

import { Component, OnInit } from '@angular/core';
import {ServiceOneService} from "../service-one.service";

@Component({
  selector: 'comp-one',
  templateUrl: './comp-one.component.html',
  styleUrls: ['./comp-one.component.css']
})
export class CompOneComponent implements OnInit {

  list;
  constructor(service: ServiceOneService) {
    this.list = service.getList();
  }

  ngOnInit() {
  }

}

服务等级

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

@Injectable({
  providedIn: 'root'
})
export class ServiceOneService {

  getList() {
    return ["List Item 1","List Item 2","List Item 3","List Item 4"];
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CompOneComponent } from './comp-one/comp-one.component';

@NgModule({
  declarations: [
    AppComponent,
    CompOneComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ServiceOneService], `// getting error here.`
  bootstrap: [AppComponent]
})
export class AppModule { }

我已经在计算机上安装了这些软件。

Npm : 6.1.0
Node : v8.9.4
Angular Version : "@angular/core": "~7.2.0",
Typescript : Version 3.1.1

有人可以在这里帮助我,在为组件导入或添加服务时出了什么问题。

1 个答案:

答案 0 :(得分:2)

您需要在app.module.ts中导入ServiceOneService

import {ServiceOneService} from "../service-one.service";