[我正在D&D角色创建器中工作,以获取乐趣和知识,但是我在这里的服务遇到了一些问题,很明显,TS编译器在我对'CharacterVariables'的理解上存在一些问题:
ERROR in src/app/components/httpGet.service.ts:15:18 - error TS2339: Property 'charVar' does not exist on type 'HttpService'.
15 this.charVar.races = response;
~~~~~~~
src/app/components/httpGet.service.ts:24:18 - error TS2339: Property 'charVar' does not exist on type 'HttpService'.
24 this.charVar.raceInfo = response;
这是它所引用的服务文件:
import { Injectable } from '@angular/core';
import { CharacterVariables } from "./CharacterVariables";
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class HttpService {
constructor(charVar: CharacterVariables, protected http: HttpClient) { }
//Get races in Character data
getRaces(route) {
this.http.get(route).subscribe(response => {
this.charVar.races = response;
}, err => {
throw err;
});
}
//Get the information of the selected race
getRaceInfo(route) {
this.http.get(route).subscribe(response => {
this.charVar.raceInfo = response;
}, err => {
throw err;
});
}
}
这是我要在服务中使用的全局变量的文件(CharacterVariables):
import { Injectable } from '@angular/core';
@Injectable()
export class CharacterVariables {
name: string = '';
surname: string = '';
gender: string = '';
races: object = {};
race: string = '';
raceInfo: object = {};
}
[我不确定TS为什么不编译它,因为如果我稍微修改一下文件(更改最小的内容,保存并撤消更改并再次保存),页面实际上就会显示出来,并且代码按预期工作,所以我对此感到困惑。有任何想法吗?谢谢。