Javascript 错误类型错误:无法读取属性';翻译';关于未定义,有人告诉我什么';怎么了?
每次我试图执行angular代码时都会出现这个错误Javascript 错误类型错误:无法读取属性';翻译';关于未定义,有人告诉我什么';怎么了?,javascript,json,angular,typescript,Javascript,Json,Angular,Typescript,每次我试图执行angular代码时都会出现这个错误 ERROR TypeError: Cannot read property 'Translation' of undefined 模型 在模型中,将构造发送到组件的数组 import { environment } from '../../../environments/environment'; export class TranslationsMessagge { constructor( ) { } pub
ERROR TypeError: Cannot read property 'Translation' of undefined
模型
在模型中,将构造发送到组件的数组
import { environment } from '../../../environments/environment';
export class TranslationsMessagge {
constructor( ) {
}
public translations() {
//filtraremos el idioma
switch (environment.language) {
//Español
case 1:
this.translation.Translation().subscribe(data => {
if (data) {
let translation: any = data;
translation = translation.body;
let tempArr: any = [];
$.each(translation.body.es, (i: any, item: { id: string; m: string; }) => {
tempArr.push({ id: item.id, m: item.m });
});
return tempArr;
}
})
break;
default:
break;
}
}
}
组成部分
在这个组件中,我只是试图展示我从模型中得到的东西
//Traducciones
import { TranslationService } from '../../services/translation/translation.service';
import { TranslationsMessagge } from '../../models/translations/translations.message.model';
@Component({
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit, AfterViewInit {
public msg:TranslationsMessagge;
constructor(private router: Router, private translations: TranslationService) {
if (localStorage.getItem('bit_token') != null) {
this.router.navigate(['Dashboard']);
this.msg= new TranslationsMessagge();
}
ngOnInit() {
console.log( this.msg.translations() );
}
我需要知道我为什么会犯这样的错误,我正在寻找解决方案,但我找不到任何有帮助的方法。
我真的需要它,怎么了?只是一个快速猜测
this.translation
在translationMessagge中未定义,但它甚至不能在类中声明。您提供的代码似乎不完整。它不允许我添加更多的代码,因为它超过了LOT,您没有声明它。这就是为什么它提供未定义的代码