加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 无法设置仅包含getter的[object Object]的属性堆栈

发布时间:2020-12-17 09:52:39 所属栏目:安全 来源:网络整理
导读:我在下面的plunker中收到以下错误. Cannot set property stack of [object Object] which has only a getter Plunker在这里 https://plnkr.co/edit/IP1ssat2Gpu1Cra495u2?p=preview 代码如下: //our root app componentimport {Component,NgModule,OnInit,I
我在下面的plunker中收到以下错误.

Cannot set property stack of [object Object] which has only a getter

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<MyModel>{
    return this.http.get('https://run.plnkr.co/j2Cw0yaD5Dn7ENaR/mymodel.json')
                .toPromise()
                .then(response => {
                                return response as MyModel;
                        });
  }
}

@Component({
  selector: 'my-app',template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,})
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行,那么它工作正常.请帮忙.

如果要在App中使用myService,则需要将其分配给构造函数中的属性或指定其可见性(TypeScript功能):
constructor(private myService : MyService) {
    this.name = 'Angular2'
}

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读