当前位置:  开发笔记 > 编程语言 > 正文

无法设置只有getter的[object Object]的属性堆栈

如何解决《无法设置只有getter的[objectObject]的属性堆栈》经验,为你挑选了1个好方法。

我在下面的plunker中收到以下错误.

无法设置只有getter的[object Object]的属性堆栈

Plunker在这里 https://plnkr.co/edit/IP1ssat2Gpu1Cra495u2?p=preview

代码如下:

//our root app component
import {Component, NgModule, OnInit, Injectable} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { HttpModule, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

class MyModel {
  public name: string;
  public value: string;
}

@Injectable()
export class MyService {

  constructor(public http: Http) {

  }

  getData (request: MyModel):Promise{
    return this.http.get('https://run.plnkr.co/j2Cw0yaD5Dn7ENaR/mymodel.json')
                .toPromise()
                .then(response => {
                                return response as MyModel;
                        });
  }
}

@Component({
  selector: 'my-app',
  template: `
    

Hello {{name}}

`, }) export class App implements AfterViewInit { name:string; constructor(myService : MyService) { this.name = 'Angular2' } ngAfterViewInit(){ let myModelObj : MyModel = new MyModel(); console.log(this.myService); this.myService.getData(myModelObj) .then(response => { console.log('GET Request success') console.log(response); }); } } @NgModule({ imports: [ BrowserModule, HttpModule ], declarations: [ App ], providers : [MyService], bootstrap: [ App ] }) export class AppModule {}

更新

我能够理解这里的错误

this.myService.getData(myModelObj)
          .then(response => {
            console.log('GET Request success')
            console.log(response);
          });

如果我评论这4行,那么它工作正常.请帮忙.



1> martin..:

如果要使用myService,则App需要将其分配给构造函数中的属性或指定其可见性(TypeScript功能):

constructor(private myService : MyService) {
    this.name = 'Angular2'
}

换句话说this.myService,在调用时未定义this.myService.getData(myModelObj).

您更新的演示:https://plnkr.co/edit/OyYUDSfD0Q8dDuwEAr1l

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有