简体   繁体   English

angular2组件刷新数据

[英]angular2 component refresh data

I have a snippet of code in my anuglar2 Component that loads the data on during the component initialization phase - I want the data to get updated during page refresh, what am I doing wrong?, 我的anuglar2组件中有一段代码,用于在组件初始化阶段加载数据-我想在页面刷新期间更新数据,我在做什么错?

here's my snippet: 这是我的片段:

   ngOnInit(): void {
        this._productService.getProducts().subscribe(
            products => this.products = products,
            error => this.errorMessage = <any>error);
    }

I think problem is with ngOnInit() . 我认为问题在于ngOnInit() It gets called once only when component is initialized. 仅在组件初始化时才调用一次。 When you refresh, ngOnInit() will not be called again.So, you should load data in constructor or in some other custom function . 刷新时不会再次调用ngOnInit(),因此,应在构造函数或其他自定义函数中加载数据。

constructor(private _productService:productService){
   _productService.getProducts().subscribe(
                                 products => this.products = products,
                                 error => this.errorMessage = <any>error);
}

OR 要么

// make sure you call it from somewhere.
GetProducts()
{ 
      this._productService.getProducts().subscribe(
            products => this.products = products,
            error => this.errorMessage = <any>error);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM