繁体   English   中英

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

[英]ERROR in src/app/app.module.ts(15,15): error TS2304: Cannot find name 'ServiceName'

我刚接触过角度并尝试逐步学习每一个步骤,我有一项服务,需要在Component中使用它,但遇到上述错误。

  1. 使用angular cli创建了一个应用程序ng new appName
  2. 使用ng gc comp-name创建了一个组件
  3. 使用ng gs service-name创建了服务
  4. 当我在Providers中的app.module.ts添加服务名称时:['service-name']。 使用ng build || ng serve代码后出现以下错误 ng build || ng serve

     Hash: 31c1abeb694df29adcbe 

    时间:8299毫秒块{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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM