无法在Angular2中创建对象数组

时间:2017-02-17 19:41:36

标签: angular typescript

这是一个简单的问题并且已经完成了很多次,但现在我无法创建一个对象数组。我自然地用谷歌搜索,下面也是他们所拥有的。

@Injectable()
export class SearchService {

    private let data: User[] = [
        {ssn: "146-89-5656", userId: 'jochampa', office: 'AMB', role: 'admin'},
        {ssn: "777-88-1000", userId: 'teddyBear', office: 'AMB', role: 'admin'},
        {ssn: "132-40-4000", userId: 'Josh', office: 'AMB', role: 'admin'},
        {ssn: "123-56-6000", userId: 'Kori', office: 'AMB', role: 'admin'},
        {ssn: "777-98-7000", userId: 'Dummy', office: 'AMB', role: 'admin'},
        {ssn: "899-19-2200", userId: 'Drew', office: 'AMB', role: 'admin'},
        {ssn: "021-69-2222", userId: 'Ken', office: 'AMB', role: 'admin'},
        {ssn: "589-47-9999", userId: 'Justin', office: 'AMB', role: 'admin'},
        {ssn: "146-89-5656", userId: 'jochampa', office: 'AMB', role: 'admin'},
        {ssn: "777-88-1000", userId: 'teddyBear', office: 'AMB', role: 'admin'},
        {ssn: "132-40-4000", userId: 'Josh', office: 'AMB', role: 'admin'},
        {ssn: "123-56-6000", userId: 'Kori', office: 'AMB', role: 'admin'},
        {ssn: "777-98-7000", userId: 'Dummy', office: 'AMB', role: 'admin'},
        {ssn: "899-19-2200", userId: 'Drew', office: 'AMB', role: 'admin'},
        {ssn: "021-69-2222", userId: 'Ken', office: 'AMB', role: 'admin'},
        {ssn: "589-47-9999", userId: 'Justin', office: 'AMB', role: 'admin'},
    ];

但是当我将鼠标悬停在ERROR TS1005 expected =

上时,我收到了错误消息

用户对象:

export class User {
    ssn:string;
    userId:string;
    lastName:string;
    office:string;
    role:string;
}

enter image description here

1 个答案:

答案 0 :(得分:2)

我不认为这是写这个private let data: User[] =..的正确方法 这是一个字段还是一个局部变量?

如果它是一个字段,那么它应该是

private data: User[] = ..

因此,您可以使用this.data

在课堂中访问它

否则它应位于contstructor方法内或@Injectable

之外

此外,对象不具有lastName属性。