我第一次使用角,我试图让一个服务运行,这样我就可以得到文件的内容。我遵循了“英雄教程”,他们解释了如何做到这一点,我也是这样做的,但出于某种原因,它不起作用。
我说ERROR TypeError: "this.fileService is undefined"时出错了,我不知道是什么问题。
about.component.ts
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
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文件的方法是错误的,但这不是我得到的错误。
怎么回事,我该怎么解决呢?
发布于 2018-09-16 18:01:03
因此,有一段时间错误没有任何意义,但我只是查看了文档,忘记了在app.module.ts中导入app.module.ts。
把它加进去后,它就修好了,它马上就起作用了。
https://stackoverflow.com/questions/52347939
复制相似问题