Angular 财产';查瓦尔';不存在于类型';HttpService';
我在D&D character creator工作是为了获得乐趣和知识,但我在这里的服务有一些问题,显然TS编译器对我的“CharacterVariables”实例有一些问题:Angular 财产';查瓦尔';不存在于类型';HttpService';,angular,typescript,Angular,Typescript,我在D&D character creator工作是为了获得乐趣和知识,但我在这里的服务有一些问题,显然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.ra
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):
我不确定TS为什么不编译这个,因为如果我对文件稍加修改(更改一些最小的内容,保存并撤消更改,然后再次保存),页面实际上会显示出来,代码会按预期工作,所以我对这个有点困惑。有什么想法吗?谢谢。要访问属性,您需要像这样声明构造函数:
constructor(private charVar: CharacterVariables, protected http: HttpClient) { }
注意我是如何添加
private
关键字的构造函数中的访问修饰符是使用Typescript定义变量的快捷方式。简而言之,写作
导出类HttpService{
构造函数(私有charVar:CharacterVariables){}
}
相当于写作
导出类HttpService{
私有charVar:CharacterVariables;
构造函数(charVar:CharacterVariables){
this.charVar=charVar;
}
}
因此,如果未提及访问修饰符,则类中没有成员变量charVar
constructor(private charVar: CharacterVariables, protected http: HttpClient) { }