【问题标题】:I want to store component.ts data in to another variable and i want to convert into Global Variable我想将 component.ts 数据存储到另一个变量中,并且我想转换为全局变量
【发布时间】:2018-09-16 09:14:02
【问题描述】:
get_update(id:any): Observable<any[]>{


      let headers = new Headers({ 'Content-Type': 'application/json'});
      let options = new RequestOptions({ headers: headers });
      return this.http.get('http://localhost:8000/vendor/'+id,options)
                        .map(response => response.json())                        
                        .catch(error => Observable.throw(error.statusText));     


}

组件.ts

ngOnInit()
{ this.service.get_update(this.user).subscribe(data => console.log(data));
}

component.ts 数据存储在另一个变量中

【问题讨论】:

  • 请准确说明您想要达到的目标、您的尝试以及为什么您尝试的方法不起作用
  • 这不是很好的做法,但您可以尝试:window.data = data;或者你可以使用localStorage来实现它。

标签: angular angular2-template angular2-services angular2-directives


【解决方案1】:

在你的类中声明一个数据成员并使用它,为了共享数据你可以使用@input()decorator @outputdecorator 或使用服务。

    export class AppComponent {
      constructor(){}
      public data=[];//


    ngOnInit()
    { this.service.get_update(this.user).subscribe(data => this.data=data);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多