我正在使用ngx-translate来管理具有ng-version =“ 4.4.7”的角度应用程序的翻译功能
我在翻译文件中具有以下密钥:
en.json :
{
"moreText": "more"
}
es.json :
{
"moreText": "más"
}
TestService.ts:
import { TranslateService } from "@ngx-translate/core";
@Injectable()
export class TestService {
public moreText: string;
constructor(private zone: NgZone, public translateService: TranslateService) {
this.moreText = translateService.instant("moreText");
}
}
我正在尝试在test.model.ts文件中使用moreText
test.model.ts:
import { TestService } from "./test.service";
export class TestModel {
constructor(data: any, private testService?: TestService) {
}
get parsedCountries(): string {
const countriesAmount = this.countries.length;
return (countriesAmount < 4
? this.countries
: this.countries
.slice(0, 2)
.concat(`${countriesAmount - 2} ${this.testService.moreText}`)
).join(", ");
}
}
我没有看到任何编译时错误,但是在朗姆酒时,在parsedCountries()中使用moreText时出现错误:ERROR TypeError:无法使用ngx-translate读取未定义的属性'moreText'>