简体   繁体   English

Angular:页面重新加载时数据消失

[英]Angular: data disappears on page reload

I've an html template我有一个 html 模板

Company-list.component.html公司列表.component.html

<br/>
<div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-6">
            <div class="card">
            <div class="card-header">
                <i class="fa fa-align-justify">Employee List</i> 
            </div>
            <div class="card-body">
                    <table class="table" >
                    <thead font-size="2px" align="center">
                        <tr>
                            <th>Id</th>
                            <th>Company </th>           
                            <th>product</th>        
                            <th>Quantity</th> 
                            <th>Price</th>                 
                        </tr>
                    </thead>
                    <tbody>
                        <tr align="center" *ngFor="let data of datas" >                            
                            <td>{{data.cmpid}}</td>  
                            <td>{{data.cmpname}}</td> 
                            <td>{{data.cmpproduct}}</td>
                            <td>{{data.cmpqty}}</td>
                            <td>{{data.cmpprice}}</td>     
                        </tr>
                    </tbody>
                    </table>
                </div>
            </div>
        </div> 
    </div>
</div>

Method that gets invoked on button click单击按钮时调用的方法

Company-list.component.ts公司列表.component.ts

public getCompanylist(){

   console.log("getCompanyList() called");
  this.datas=this.newservice.getcompanylist();
    console.log(this.datas);  
    console.log("getcomapnylist() in companylistcomponent.ts Called da");
    return this.datas;
  };

The same method gets invoked on page load as well在页面加载时也会调​​用相同的方法

Company-list.component.ts公司列表.component.ts

ngOnInit() {

    console.log("NG oninit called.")
    this.getCompanylist();

  }

getCompanylist() inturn calls a method in the service getCompanylist() 反过来调用服务中的方法

myservice.ts myservice.ts

getcompanylist() {

  alert("service method called");   

  this.http.get("http://localhost:8080/tabletolist").subscribe((data)=>{this.datas=data});
return this.datas;
}

When I click the button getCompanyList() method updates datas field with array returned from the springboot server as expected but on page load eventhough same method ie getCompanylist() is called it sets datas field to undefined instead of data returned from the server.当我单击按钮时, getCompanyList()方法会按预期使用从 springboot 服务器返回的数组更新datas字段,但在页面加载时,即使调用了相同的方法,即getCompanylist()它将datas字段设置为未定义,而不是从服务器返回的数据。 Even weird, when the component is loaded using the router from another component, it works as expected but on loading the same page url even though method is invoked it sets datas field to undefined.更奇怪的是,当组件使用路由器从另一个组件加载时,它按预期工作,但在加载相同的页面 url 时,即使调用了方法,它也会将数据字段设置为未定义。 Someone please help me figure out the issue.有人请帮我弄清楚这个问题。

You can use the datas variable in your service instead of returning it.您可以在服务中使用 data 变量而不是返回它。

(It looks like the function can return before the subscribe call is complete in your original way). (看起来该函数可以在以原始方式完成订阅调用之前返回)。

Eg:例如:

myservice.ts myservice.ts

getcompanylist() {

  this.http.get("http://localhost:8080/tabletolist").subscribe((data)=>{
{
this.datas=data
}
);
}

Company-list.component.ts:公司列表.component.ts:

ngOnInit(){
this.newservice.getcompanylist();
  };

Company-list.component.html公司列表.component.html

<tr align="center" *ngFor="let data of newservice.datas" > 
...
</tr>

Your coding practice is not recommended and must be improved.不推荐您的编码实践,必须改进。

Try this approach:试试这个方法:

1-Service should be used for service/api calls and data-flow. 1-Service 应该用于服务/api 调用和数据流。

2-Service Call Subscription should be handled where service is required mainly components not the service component itself. 2-服务调用订阅应该在需要服务的地方处理,主要是组件而不是服务组件本身。

Your code should be:你的代码应该是:

myservice.ts myservice.ts

getCompanyList() {
  const url = "http://localhost:8080/tabletolist";
  return this.http.get(url);
}

Company-list.component.ts公司列表.component.ts

Declare any empty array property namely datas声明任何空数组属性,即数据

public datas = [];

Initialize your service in component's constructor (private myService: myservice)在组件的构造函数中初始化您的服务(private myService: myservice)

getCompanyList(){
   this.myService.getCompanyList().subscribe(res => {
          this.datas = res
      });
}

Call getCompanyList Method when component is initialised组件初始化时调用 getCompanyList 方法

ngOnInit() {
    this.getCompanyList();
}

myservice.ts myservice.ts

getcompanylist() {

  alert("service method called");   

  this.http.get("http://localhost:8080/tabletolist").subscribe((data)=> data);
}

try like this像这样尝试

Company-list.component.ts公司列表.component.ts

ngOnInit() {

    console.log("NG oninit called.")
   this.newservice.getcompanylist().then((data) => { this.datas = data});

  }

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

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