简体   繁体   English

angular2打字稿无法分配给this.object属性

[英]angular2 typescript cannot assign to this.object property

I am attempting a project in angular2 and typescript, and I am having some inexplicable problems. 我正在尝试使用angular2和Typescript项目,但遇到一些莫名其妙的问题。 For some reason, I cannot assign data to a class property. 由于某些原因,我无法将数据分配给类属性。

I have a pair of class properties defined like this: 我有一对这样定义的类属性:

public loadResultMessages: string[];
public book: Book;

I have a method where I'm calling a service like this: 我有一种方法可以像这样调用服务:

getBookByCode() {

    this.http.get('/api/book/loadbycode?code=' + this.bookCode)
        .subscribe(result => {

            var object = result.json();

            console.log(object);

            this.loadResultMessages = object.loadResultMessages;
            this.book = object.book;

    });
}

I have verified that I am getting data back in the object, but even after attempting to assign data to the two properties, they say undefined. 我已经验证我在对象中获取了数据,但是即使在尝试将数据分配给这两个属性后,它们仍未定义。 How can that be? 怎么可能?

EDIT: Okay, I am messing around with Visual Studio 2017 and setting breakpoints in my JavaScript. 编辑:好的,我搞砸了Visual Studio 2017,并在我的JavaScript中设置了断点。 I was mousing over the variables and it was telling me they were undefined. 我将鼠标悬停在变量上,并告诉我它们是未定义的。 However, when I actually log them out to the console they are populated. 但是,当我实际将它们注销到控制台时,它们已被填充。 Other variables I can mouse over and they show the results. 我可以将鼠标悬停在其他变量上,并显示结果。 I'm not sure if this is some kind of timing issue or a bug in VS? 我不确定这是计时问题还是VS中的错误?

I think I'm also suffering some major caching issues. 我想我也遇到了一些重大的缓存问题。 Just putting the extra console.log() calls in there suddenly has made my code start working. 仅仅在其中添加了额外的console.log()调用,就使我的代码开始工作了。

Mostly your service should be returning an Observable of an object or array of objects. 通常,您的服务应该返回一个对象或对象数组的Observable。

getBookByCode(bookCode) : Observable<any[]> {

    return this.http.get('/api/book/loadbycode?code=' + bookCode)
                    .map((response: Response) => <any[]>response.json())
                    .do(data => console.log("data we got is " + JSON.stringify(data)))
                    .catch(this.handleError);
    });
}

In your component you should be subscribing to it in your constructor as 在您的组件中,您应该在构造函数中订阅它,如下所示:

constructor(private serviceObject:ServiceName){
    this.serviceObject.getBookByCode(this.bookCode)
                    .subscribe(results => this.results = results,
                    error =>console.log(error);
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2 / Typescript / ngRx - TypeError:无法分配给对象的只读属性 - Angular2/Typescript/ngRx - TypeError: Cannot assign to read only property of object Angular2 / Typescript对象和属性绑定到模板 - Angular2/Typescript object and property binding to template Angular2收到错误TypeError:无法分配为只读对象&#39;#的属性&#39;list&#39; <HTMLInputElement> “ - Angular2 getting ERROR TypeError: Cannot assign to read only property 'list' of object '#<HTMLInputElement>' Angular2对象不能设置undefined的属性 - Angular2 object cannot set property of undefined TypeScript / Angular2-例外:TypeError:无法读取未定义的属性“ push” - TypeScript / Angular2 - EXCEPTION: TypeError: Cannot read property 'push' of undefined Angular2 - TypeError:无法读取(Typescript)中未定义的属性“Id” - Angular2 - TypeError: Cannot read property 'Id' of undefined in (Typescript) Typescript / Angular2 TypeError:无法读取undefined的属性 - Typescript/Angular2 TypeError: Cannot read property of undefined 无法读取Angular2 Typescript中未定义的属性“值” - Cannot read property 'value' of undefined in Angular2 Typescript 找不到属性angular2和打字稿 - Property not found angular2 and typescript TypeError:无法分配给 typescript 中的 object '[object Array]' 的只读属性 '0' - TypeError: Cannot assign to read only property '0' of object '[object Array]' in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM