首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度6:服务未定义

角度6:服务未定义
EN

Stack Overflow用户
提问于 2018-09-15 19:02:11
回答 1查看 6.6K关注 0票数 7

我第一次使用角,我试图让一个服务运行,这样我就可以得到文件的内容。我遵循了“英雄教程”,他们解释了如何做到这一点,我也是这样做的,但出于某种原因,它不起作用。

我说ERROR TypeError: "this.fileService is undefined"时出错了,我不知道是什么问题。

about.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core'
import { FileService } from './../file.service'

declare var require:any;
const markdown = require('markdown').markdown;

@Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.scss']
})
export class AboutComponent implements OnInit {

  constructor(private fileService:FileService) { }

  ngOnInit() {
    const aboutmeraw = this.fileService.readFile("./../assets/md/aboutme.md");
    this.aboutme = markdown.toHTML(aboutmeraw);
  }

  aboutme:string;

}

file.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

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

  constructor(private http:HttpClient) { }

  public readFile(file:string):Observable<any> {
    return this.http.get(file);
  }
}

我知道返回HTTP文件的方法是错误的,但这不是我得到的错误。

怎么回事,我该怎么解决呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-16 18:01:03

因此,有一段时间错误没有任何意义,但我只是查看了文档,忘记了在app.module.ts中导入app.module.ts

把它加进去后,它就修好了,它马上就起作用了。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52347939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档