在Angular 8中,如果出现错误“全局变量 this.chats 未定义”,可能是因为在组件中没有正确声明和初始化该变量。下面是一个解决方法的示例代码:
在组件中声明和初始化this.chats变量:
export class YourComponent implements OnInit {
chats: any[] = [];
ngOnInit() {
// 初始化 this.chats 变量
this.chats = [
{ id: 1, name: 'Chat 1' },
{ id: 2, name: 'Chat 2' },
{ id: 3, name: 'Chat 3' }
];
}
}
确保在组件模板中正确引用this.chats变量:
{{ chat.name }}
这样就可以解决“全局变量 this.chats 未定义”错误,并且在模板中正确使用this.chats变量。请根据你的实际需求修改代码。